Url regex for preg_replace, but without matching URLs in bbcode [url]

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://www.google.com
[url]http://www.google.com[/url]
[url="http://www.google.com"]http://www.google.com[/url]

need to be replaced with one line:

<a href="http://www.google.com">http://www.google.com</a>

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]?

.

+3
2

. , , .

:

bbcode, , URL-, :

"normal text", "bbcode", "normal text", "bbcode"

bbcode URL- "bbcode", URL-- " ".

, , .

Voila, .

+1

[url] BBCodes, URL- . lookbehind, , URL- . , &quot;, URL .

0

All Articles