note 27203 deleted from function.preg-replace by sniper

From: Date: Sun, 07 Sep 2003 22:04:24 +0000
Subject: note 27203 deleted from function.preg-replace by sniper
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-55695@lists.php.net to get a copy of this message
Note Submitter: mdreed@mac.com ---- 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)); }

« previous php.notes (#55695) next »