im by creating a comment section on the website. First I needed to make a regex that would find any url and replace it with
<a href="url"></a>
So, I found a super regex to find all the url in the comment, and I made a function that returns all the urls with the html tag:
function addURLTags($string) {
$pattern = "/(?i)\b((?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'\".,<>?«»""‘’]))/";
return preg_replace($pattern, '<a href="$1">$1</a>', $string);
}
Everything went perfectly. but a week ago my boss told me that now I need to add bbcode to the comments section. And I was like “no problem” ... but then he told me that my addURLTags function should remain.
So any line like this:
http:
[url]http:
[url="http://www.google.com"]http:
need to be replaced with one line:
<a href="http://www.google.com">http:
So, I have a small php library that replaces all bbcode ocurrences with html code.
: ", first url-, [url] , , -, bbcode
, :
/\ (([URL]) (: https: (?)!? | [.]//WWW\d {0,3} | [-z0-9.-] + [.] [AZ] {2,4}/): < > ] + | (([^\s() < > ] + | (([^\s() & ([^\ (?); > ] +))))) + (?: (([^\s() & ; > ] + | (([^\s() < > ] +)))) | [^\s`!() [] {};: ' ".?, < > "" "\"']))/
!
, -, , . , .
- ? - PHP-, " url" bbcode [url]?
.