views:

836

answers:

5

I have a simple commenting system here...

http://affbuzz.com/comments/7299a55137def55917a5dc6c4fe0f261af8a4217

...and people can submit hyperlinks inside the plain text field. When I display these records back from the database and into the web page, what RegExp in PHP can I use to convert these links into HTML-type anchor links?

Bonus: For the algorithm to not do this with any other kind of link, just http and https.

+1  A: 
<?
function makeClickableLinks($text)
{

        $text = html_entity_decode($text);
        $text = " ".$text;
        $text = eregi_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)',
                '<a href="\\1" target=_blank>\\1</a>', $text);
        $text = eregi_replace('(((f|ht){1}tps://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)',
                '<a href="\\1" target=_blank>\\1</a>', $text);
        $text = eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)',
        '\\1<a href="http://\\2" target=_blank>\\2</a>', $text);
        $text = eregi_replace('([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})',
        '<a href="mailto:\\1" target=_blank>\\1</a>', $text);
        return $text;
}

// Example Usage
echo makeClickableLinks("This is a test clickable link: http://www.websewak.com  You can also try using an email address like [email protected]");
?>
Luca Matteis
@Luca, Are you aware that "eregi_replace" and the other POSIX regex functions are deprecated?
Alan Moore
`preg_replace` seems the standard regex function now.
seanmonstar
+1  A: 

I recommend not to do many things on fly like this. I prefer to use simple editor interface like the one used in stackoverflow. It is called Markdown.

Yousf
StackOverflow automatically parses links. `http://stackoverflow.com` (http://stackoverflow.com).
Alix Axel
Markdown is a good solution if you don't mind all the other markup it injects as well (UL, P, EM, STRONG, and CODE tags to name a few). But if all you want/need is linkification than it's easier to roll your own.
broofa
+1  A: 
public static function makeClickableLinks($s) {
    return preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.-]*(\?\S+)?)?)?)@', '<a href="$1">$1</a>', $s);
}
Volomike
A: 

I like voloMike's answer, but it doesn't work if a period is directly following the hyperlink. for example, if the text ended with: http://www.google.com/.

In that case, the . would get appended to the href="" attribute and would also be inside of the tag's inner HTML.

Granted, most servers will still deliver the page if there is an extra dot in there, but it still bugs me and also causes problems for some versions of the drupal cms.

thanks!

+1  A: 

Well, Volomike's answer is much closer. And to push it a bit further, here's what I did for it to disregard the trailing period at the end of the hyperlinks. I also considered URI fragments.

public static function makeClickableLinks($s) {
  return preg_replace('@(https?://([-\w\.]+[-\w])+(:\d+)?(/([\w/_\.#-]*(\?\S+)?[^\.\s])?)?)@', '<a href="$1" target="_blank">$1</a>', $s);
}
mOkker