note 34999 modified in function.preg-replace by vincent
| From: | vincent@php.net | Date: | Mon, 25 Aug 2003 12:20:31 +0000 |
| Subject: | note 34999 modified in function.preg-replace by vincent | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-55031@lists.php.net to get a copy of this message | ||
The code below is a bit modified variant of one of automatic-hyperlinkers above (accepts http,
https, ftp protocol URLs, also email address), this one also accpets Apache /~user functionality:
$text = preg_replace("/(?:^|\b)((((http|https|ftp):\/\/)|(www\.))
([\w\.]+)([,:%#&\/?~=\w+\.-]+))(?:\b|$)/is",
"<a href=\"$1\" target=\"_blank\">$1</a>", $text);
$text = preg_replace("/(([\w\.]+))(@)([\w\.]+)\b/i","<a href=\"mailto:$0\">$0</a>",$text);
If you have a string with tags , including for example <img src=http:///www.qwe.ee/img.jpg>, you might want to add negative
lookbehinds, (?<!\/)(?<!=) before ((((http|https|ftp).
This excludes URLs, which begin with / and =
--was--
The code below is a bit modified variant of one of automatic-hyperlinkers above (accepts http,
https, ftp protocol URLs, also email address), this one also accpets Apache /~user functionality:
$text = preg_replace("/(?:^|\b)((((http|https|ftp):\/\/)|(www\.))
([\w\.]+)([,:%#&\/?~=\w+\.-]+))(?:\b|$)/is",
"<a href=\"$1\" target=\"_blank\">$1</a>", $text);
$text = preg_replace("/(([\w\.]+))(@)([\w\.]+)\b/i","<a href=\"mailto:$0\">$0</a>",$text);
http://www.php.net/manual/en/function.preg-replace.php