match url pattern in php using regular expression

Eduptuppy

New Member
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:[*]\[code\]http://example.com\[/code\][*]\[code\]https://example.com\[/code\][*]\[code\]www.example.com\[/code\][*]\[code\]example.com\[/code\]\[code\]preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@', '<a href="http://stackoverflow.com/questions/3904482/$1">$1</a>', $subject);\[/code\]This expression matches only first two types of url.If I use this expression for match url pattern\[code\]'@(www?([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@'\[/code\], 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?
 
Back
Top