I want to match a URL link in a wall post and replace this link with anchor tag. For this I use the regular expression below.
I would like the match four types of URL:
http://example.comhttps://example.comwww.example.comexample.com
preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@',
'<a href="$1">$1</a>', $subject);
This expression matches only first two types of URL.
If I use this expression for matching a URL pattern,
'@(www?([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@', it only matches the third type of URL pattern.
How can I match all four typeS of URL patternS with a single regular expression?
I’d use a different regex to be honest. Like this one that Gruber posted in 2009:
Or this updated version that Gruber posted in 2010 (thanks, @IMSoP):