im making a comment section on a website. At first I needed to do a regular expression that finds any url and replace it surrounded with
<a href="url"></a>
So I found a super regular expression to find all the url's in a comment and I did 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 excellent. but one week ago my boss told me that now I have to add bbcode to the comment section. And I was like "no problem"... but then he told me that my function addURLTags has to stay.
So any string like this:
http://www.google.com
[url]http://www.google.com[/url]
[url="http://www.google.com"]http://www.google.com[/url]
must be replaced to the same string:
<a href="http://www.google.com">http://www.google.com</a>
So I got a little php library that replaces all bbcode ocurrences to html code.
And I thought: "Ok, first I should get all url ocurrences that do not have a [url] tag in the beggining! And second I replace all the bbcode tags"
And I tried to add a negative assertion at the beggining of the super regex, something like this:
/(?i)\b((?![url])(?:https?://|www\d{0,3}[.]|[a-z0-9.-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|(([^\s()<>]+|(([^\s()<>]+)))))+(?:(([^\s()<>]+|(([^\s()<>]+))))|[^\s`!()[]{};:'\".,<>?«»“”‘’]))/
but didnt work!
Im kinda newbie with regular expressions and all the online testers I tried do not work well with such a long regex. I dont know what else try.
Do you have any suggestion? Do you know any PHP lybrary that does the "url replacing" with and without the [url] bbcode tags?
Thank you in advance.