note 77787 added to function.preg-replace

From: Date: Thu, 13 Sep 2007 02:37:27 +0000
Subject: note 77787 added to function.preg-replace
Groups: php.notes 
Request: Send a blank email to php-notes+get-130445@lists.php.net to get a copy of this message
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 ---- Server IP: 66.207.199.35 Probable Submitter: 24.207.45.110 ---- Manual Page -- http://www.php.net/manual/en/function.preg-replace.php Edit -- https://master.php.net/note/edit/77787 Del: integrated -- https://master.php.net/note/delete/77787/integrated Del: useless -- https://master.php.net/note/delete/77787/useless Del: bad code -- https://master.php.net/note/delete/77787/bad+code Del: spam -- https://master.php.net/note/delete/77787/spam Del: non-english -- https://master.php.net/note/delete/77787/non-english Del: in docs -- https://master.php.net/note/delete/77787/in+docs Del: other reasons-- https://master.php.net/note/delete/77787 Reject -- https://master.php.net/note/reject/77787 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#130445) next »