URL and email to anchor conversion
Posted: Wed Jul 27, 2005 6:23 am
Found this lovely function ealier, so thought I'd post it up, maybe someone will find it useful!
Code: Select all
//Function converts plain-text email addresses and URLs to hyperlinks
function autolink($str) {
// Email
$pattern[0] = "/([A-Za-z0-9\+\-_]+)@([A-Za-z0-9-_]+)\.([A-Za-z]{2,4})([,.]?)/";
$replace[0] = '<a href="mailto:$1@$2.$3">$1@$2.$3</a>$4';
// URL - http
$pattern[1] = "/(?<!\")(http|https|ftp):\/\/([^\s]*)/i";
$replace[1] = '<a target="_blank" href="$1://$2">$1://$2</a>';
// URL - www
$pattern[2] = '/(?<!http:\/\/)(www\.)([^\s]*)/i';
$replace[2] = "<a href=\"http://www.$2\" target=\"_blank\">www.$2</a> ";
return preg_replace($pattern, $replace, $str);
}