note 27203 added to function.preg-replace
| From: | mdreed at mac dot com | Date: | Wed, 27 Nov 2002 06:15:11 +0000 |
| Subject: | note 27203 added to function.preg-replace | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-40021@lists.php.net to get a copy of this message | ||
Here's the function I am using for encoding HTML from plain text. It's gathered from a
bunch of other comments on this page. I know for a fact that it works (at least on my box), so you
don't have to worry about inaccurate links. I know of no bugs (it properly excludes periods at
the end of links, etc). It's not pretty, but it's always witty to have a one line
function that does real work.
function HTMLEncode($text) {
return nl2br(preg_replace(array("/(([\w\.]+))(@)([\w\.]+)\b/i",
"/((ftp(7?):\/\/)|(ftp\.))([\w\.\/\&\=\?\-]+)/",
"/((http(s?):\/\/)|(www\.))([\w\.\/\&\=\?\-]+)/"), array("<a href=\"mailto:$0\" style=\"color:blue;
text-decoration:none\">$0</a>", "<a href=\"ftp$3://$4$5\"
target=\"_blank\" style=\"color:blue;
text-decoration:none\">$4$5</a>", "<a href=\"http$3://$4$5\"
target=\"_blank\" style=\"color:blue;
text-decoration:none\">$4$5</a>"), $text));
}
--
http://www.php.net/manual/en/function.preg-replace.php
http://master.php.net/manage/user-notes.php?action=edit+27203
http://master.php.net/manage/user-notes.php?action=delete+27203
http://master.php.net/manage/user-notes.php?action=reject+27203