note 77787 deleted from function.preg-replace by danbrown

From: Date: Fri, 28 Nov 2008 19:47:39 +0000
Subject: note 77787 deleted from function.preg-replace by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-147142@lists.php.net to get a copy of this message
Note Submitter: iasmin at amazingdiscoveries dot org ---- I thought that someone could use this hyperlink function. preg_replace is about 6 times faster than ereg_replace. I took the original example from the ereg_replace function page and modified so that it works perfect. I gave a comment of what it matches. One thing is that I added a space at the beginning so that only links that don't have <a href="" around them or anything else touching will be replaced. <i>NOTE! I had to break the long lines otherwise I couldn't have posted this. So take the new line out and it will work</i> <?php function hyperlink(&$text) { // match protocol://address/path/file.extension?some=variable&another=asf% $text = preg_replace("/\s(([a-zA-Z]+:\/\/)([a-z][a-z0-9_\..-]* [a-z]{2,6})([a-zA-Z0-9\/*-?&%]*))\s/i", " <a href=\"$1\">$3</a> ", $text); // match www.something.domain/path/file.extension?some=variable&another=asf% $text = preg_replace("/\s(www\.([a-z][a-z0-9_\..-]* [a-z]{2,6})([a-zA-Z0-9\/*-?&%]*))\s/i", " <a href=\"http://$1\">$2</a> ", $text); return $text; } ?> Play around with it and see how it works. Courtesy of AmazingDiscoveries.org God bless, Iasmin Balaj

« previous php.notes (#147142) next »