function make_clickable($text)
{
$ret = ' ' . $text;
$ret = preg_replace("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t<]*)#ise", "'\\1<a href=\"\\2\" >\\2</a>'", $ret);
$ret = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r<]*)#ise", "'\\1<a target=\"_blank\" href=\"http://\\2\" >\\2</a>'", $ret);
$ret = preg_replace("#(^|[\n ])([a-z0-9&\-_\.]+?)@([\w\-]+\.([\w\-\.]+\.)*[\w]+)#i", "\\1<a href=\"mailto:\\2@\\3\">\\2@\\3</a>", $ret);
$ret = substr($ret, 1);
return($ret);
}
- Problems:
http://www.google.com ,
http://www.google.com ;
http://www.google.com .
http://www.google.com :
http://www.google.com ?
- (http://www.google.com)
as well as make sure that other domains like these still work:
I am not very fluent in regex at all and I stole this script from somewhere on the internet.
I know there are limitations to regex and this may be one, but any help at all would be greeaatly appreciated. I also notice that this site is using some nice javascript to parse urls really nicely. It worked on every one of my "problem" domains except for the one with (). Can anyone show me where stackoverflow's JS parser is? I was unable to locate it.
One more question: I am doing this for a newspaper site (to automatically parse links that the authors may write in their stories, as well as automatic mailto email addresses). I am thinking that it might be better to use javascript and let each client machine render the links that way. But I also want it to be reliable, so cross browser issues and things like noscript may come into play that way. Any thoughts?
Seems to me you want the starting (^|[\n ]) replaced with \b, and a zillion other problems... Possibly change: [^ \"\n\r\t<]* to (\w|\W(?=\w|$)) for the second one:
ReplyDeletepreg_replace('#\b(www|ftp)\.(\w|\W(?=\w|$))+#ise', '<a target="_blank" href="http://\\0" >\\0</a>', $ret);
... but thats just to get you started... It is no easy matter, and I'm not willing to put in the time to make it more full proof ;)
There's no way to make your current approach standards-compliant, and I can't be bothered either. Since you are just asking for the blackbox/magic regex codez, a simple workaround would be a negative assertion:
ReplyDelete(?<![.?;:)])
Add that in your regex right before the #ise, so it won't match those characters at the very end.