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:
This works great for me - including mailto check:
function LinkIt($text)
{
$t = preg_replace("/(\b(?:(?:http(s)?|ftp):\/\/|(www\.)))([-a-züöäß0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|])/im", '<a target="_blank" href="http$2://$3$4" class="external-link" title="External Link">$1$4</a>', $text);
return preg_replace("/([\w+\.\-]+@[\w+\-]+\.[a-zA-Z]{2,4})/im", strtolower('<a href="mailto:$1" class="mail" title="E-Mail">$1</a>'), $t);
}
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
(https|http|ftp)\:\/\/|([a-z0-9A-Z]+\.[a-z0-9A-Z]+\.[a-zA-Z]{2,4})|([a-z0-9A-Z]+\.[a-zA-Z]{2,4})|\?([a-zA-Z0-9]+[\&\=\#a-z]+)
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+):
$str = "ftp://www.web.com, web.net, www.website.info, website.us, web.ws?query=true, www.web.biz?query=true, ftp://web.in?query=true, media.google.com hello world, working more with ns ns.google.pk or ww1.smart.au and www3.smart.br w1.smart.so ?ques==two&t=p http://website.info?ques==two&t=p https://www.weborwebsite.com and ftp://www.hotmail.br";
echo preg_replace("/(https|http|ftp)\:\/\/|([a-z0-9A-Z]+\.[a-z0-9A-Z]+\.[a-zA-Z]{2,4})|([a-z0-9A-Z]+\.[a-zA-Z]{2,4})|\?([a-zA-Z0-9]+[\&\=\#a-z]+)/i", "", $str);
it will return
, , , , , , , hello world, working more with ns or and and
If you want to make that one work you need to make the "https?//" part optional, since you seem to have a fairly good grasp of regexps I won't show you, an excerise for the reader :)
But I generally agree with Nev, it's overly complicated for what it does.
A complete working example using Nev Stokes given link:
public function clickableUrls($html){
return $result = preg_replace(
'%\b(([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/)))%s',
'<a href="$1">$1</a>',
$html
);
}
I looked around and didn't see any that were exactly what I needed. I found this one that was close, so i modified it as follows:
^((([hH][tT][tT][pP][sS]?)\:\/\/)?([\w\\-]+(\[\w\.\&%\$\-]+)*)?((([^\s\(\)\<\>\\\"\.\ [\]\,;:]+)(\.[^\s\(\)\<\>\\\"\.\[\]\,;:]+)*(\.[a-zA-Z]{2,4}))|((([01]?\d{1,2}|2[0-4]\d|25[0-5])\.){3}([01]?\d{1,2}|2[0-4]\d|25[0-5])))(\b\:(6553[0-5]|655[0-2]\d|65[0-4]\d{2}|6[0-4]\d{3}|[1-5]\d{4}|[1-9]\d{0,3}|0)\b)?((\/[^\/][\w\.\,\?\'\\\/\+&%\$#\=~_\-]*)*[^\.\,\?\"\'\(\)\[\]!;<>{}\s\x7F-\xFF])?)$
check it out on debuggex.
use this pattern .
$regex = "(https?\:\/\/|ftp\:\/\/|www\.|[a-z0-9-]+)+([a-z0-9-]+)\.+([a-z]{2,4})((\/|\.)+([a-z0-9-_.\/]*)$|$)";