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?
I just checked this post (after 2 years) might be you got the answer but for those who are beginners, you can use regular expression to strip every type of URL or Query String
it will strip every type of URLs, take a look at the following list. I used different type of domains for those who want to ask "will it strip .us, .in or .pk etc type of domains or not.
Working Example (tested in PHP5+, Apache2+):
it will return
Hope it helps a lot of coders out there