I want to match a url link in wall post and replace this link with anchor tag, for this I use the regular expression below.
I would like the match 4 types of url:
http://example.com
https://example.com
www.example.com
example.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 match url pattern
'@(www?([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@'
, then it only matches the third type of url pattern.
How can I match all four type of url pattern with a single regular expression?