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.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 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?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…