note 12614 deleted from function.ereg-replace by jimw
| From: | jimw@php.net | Date: | Fri, 02 Nov 2001 01:57:06 +0000 |
| Subject: | note 12614 deleted from function.ereg-replace by jimw | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-20210@lists.php.net to get a copy of this message | ||
Played a bit with the ereg_replace, and this is what I came up with when trying to replace
hyperlinks and email-addresses in strings, with actual links.<br>
The function works with http://, https:// and ftp://
links, with or without the 'www'. Also works with www links without the http:// part, links with trailing punctuation, links without leading whitespace,
'weird' links (ie 'http://foo.bar/index.php?foo=bar&foobar=barfoo')
etc.<br><br> Give it a shot :-)
<br>
function urltext($str) {
$str = ereg_replace("((www.)([a-zA-Z0-9@:%_.~#-\?&]+[a-zA-Z0-9@:%_~#\?&/]))",
"http://\\1", $str);
$str = ereg_replace("((ftp://|http://|https://){2})([a-zA-Z0-9@:%_.~#-\?&]+[a-zA-Z0-9@:%_~#\?&/])",
"http://\\3", $str);
$str = ereg_replace("(((ftp://|http://|https://){1})[a-zA-Z0-9@:%_.~#-\?&]+[a-zA-Z0-9@:%_~#\?&/])",
"[<A HREF=\"\\1\" TARGET=\"_blank\">\\1</A>]", $str);
$str = ereg_replace("([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})","[<A
HREF=\"mailto:\\1\">\\1</A>]", $str);
return $str;
}
<br>