/ Published in: PHP
A really handy snippet that will replace textual URLs with HTML links.
Also works for email addresses.
Also works for email addresses.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
function replace_links( $text ) { $ret = ' ' . $text; // Replace Links with http:// $ret = preg_replace("#(^|[\n ])([\w]+?://[\w\#$%&~/.\-;:=,?@\[\]+]*)#is", "\\1<a href=\"\\2\" target=\"_blank\" rel=\"nofollow\">\\2</a>", $ret); // Replace Links without http:// $ret = preg_replace("#(^|[\n ])((www|ftp)\.[\w\#$%&~/.\-;:=,?@\[\]+]*)#is", "\\1<a href=\"http://\\2\" target=\"_blank\" rel=\"nofollow\">\\2</a>", $ret); // Replace Email Addresses $ret = preg_replace("#(^|[\n ])([a-z0-9&\-_.]+?)@([\w\-]+\.([\w\-\.]+\.)*[\w]+)#i", "\\1<a href=\"mailto:\\2@\\3\">\\2@\\3</a>", $ret); return $ret; }
URL: http://www.apashley.co.uk/snippets/40/Replace-Urls-With-Links--Php.html