note 19519 added to function.preg-replace

From: Date: Fri, 01 Mar 2002 16:17:47 +0000
Subject: note 19519 added to function.preg-replace
Groups: php.notes 
Request: Send a blank email to php-notes+get-27233@lists.php.net to get a copy of this message
I expnaded on some of the examples here to make all URL's and emails on our site links. I have found this set of replacements covers virtually all web addresses and email addresses. I am sure it could be done in a more concise manner, but this works. I found the examples previously posted missed a lot of addresses encountered on our site. $thisArticleText = preg_replace("/([^\w\/])(www\.[a-z0-9\-]+\.[a-z0-9\-]+)/i", "$1http://$2", $thisArticleText); //make sure there is an http:// on all URLs $thisArticleText = preg_replace("/([\w]+:\/\/[\w-?&;#~=\.\/\@]+[\w\/])/i", "<A TARGET=\"_blank\" HREF=\"$1\">$1</A>", $thisArticleText); //make all URLs links $thisArticleText = preg_replace("/([\w-?&;#~=\.\/]+\@(\[?)[a-zA-Z0-9\-\.]+\. ([a-zA-Z]{2,3}|[0-9]{1,3})(\]?))/i", "<A HREF=\"mailto:$1\">$1</A>", $thisArticleText); //make all emails hot links NOTE: I had to put an extra line feed in the email expression to get this forum to accept my post. You will have to take it out in order for it to work -- http://www.php.net/manual/en/function.preg-replace.php http://master.php.net/manage/user-notes.php?action=edit+19519 http://master.php.net/manage/user-notes.php?action=delete+19519 http://master.php.net/manage/user-notes.php?action=reject+19519

« previous php.notes (#27233) next »