View Full Version : link finder regex

11-01-2007, 06:04 AM
function findLinks($string){
// link URLs
$string =
eregi_replace("(([[:alnum:]]+://)|www\.)([^[:space:]]*)([[:alnum:]#?/&=])", "<a target=\"_blank\" href=\"\\1\\3\\4\" title=\"Go to \\1\\3\\4\">\\1\\3\\4</a> ", $string);

// fix lazy URLs
$string = eregi_replace("href=\"www","href=\"http://www", $string);

// link mailtos
$string = eregi_replace("(([a-z0-9_]|\\-|\\.)+@([^[:space:]]*)([[:alnum:]-]))",
"<a href=\"mailto:\\1\" title=\"E-mail \\1\">\\1</a> ", $string);

// truncates long urls that can cause display problems
$string =
">\\1\\3...\\5\\6<", $string);

$string = stripslashes($string);
return $string;

this seems to work really well as long as someone has a space after the URL (it doesnt work if someone has a linebreak after a URL)... it just doesnt put </a> before <br> :(

11-01-2007, 05:05 PM
Wow, that is the most elaborate link finder I have seen. Looks like wasted effort. It's also old (since it uses eregi_replace).

Search these boards, this question has been asked many times before. Or use a search engine. There are much better solutions to this problem. This one ranks low on the scale.

11-01-2007, 11:33 PM
i have searched many times on google and this one was the only one that worked with almost any link type... other ones are simpler but they dont detect every link submitted.