note 27673 added to function.preg-replace

From: Date: Sun, 15 Dec 2002 06:56:37 +0000
Subject: note 27673 added to function.preg-replace
Groups: php.notes 
Request: Send a blank email to php-notes+get-40922@lists.php.net to get a copy of this message
Someone pointed out to me that there is a slight problem with my HTMLEncode function. It won't, for example, encode "http://www.example.com/~root/" correctly (it'll only make "http://www.example.com/" a link and leave "~root/" as text). Here's the fixed version: 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)); } If anyone finds any more problems with this function, please tell me so I can fix them. I would imagine that there are plenty of websites that use this type of function, so any problems with it have the potential for widespread slight inconvenience! -- http://www.php.net/manual/en/function.preg-replace.php http://master.php.net/manage/user-notes.php?action=edit+27673 http://master.php.net/manage/user-notes.php?action=delete+27673 http://master.php.net/manage/user-notes.php?action=reject+27673

« previous php.notes (#40922) next »