Re: Regular Expressions

From: Date: Wed, 07 Nov 2007 14:42:57 +0000
Subject: Re: Regular Expressions
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-264181@lists.php.net to get a copy of this message
On Wed, 2007-11-07 at 14:33 +0000, Mark Summers wrote: > This is a first attempt but the general idea is that the regular > expression matching is done in one operation and then str_replace() is > called only as many times as required instead of once for every line in > your list. Also, str_replace() is faster than ereg_replace(). > > <?php > > $replace = array( > "&ntilde" => "n", > "&aacute" => "a", > "&eacute" => "e", > "&iacute" => "i", > "&oacute" => "o", > "&uacute" => "u" > ); > > $link = "ssrsrsrs&oacuteererrere&iacutesdd&oacutesdss&uacute"; > > if (preg_match_all("/(".join("|", array_keys($replace)).")/", > $link, > $matches)) { > $matches = array_unique($matches); > > foreach ($matches[0] as $match) { > $link = str_replace($match, $replace[$match], $link); > } > } > > echo $link; > > ?> Don't do this, it's terribly inefficient and superfluously complicated. There's no escaping of the strings either before jamming them into the pattern. What happens if you need to replace '||||||||'. Cheers, Rob. -- ........................................................... SwarmBuy.com - http://www.swarmbuy.com Leveraging the buying power of the masses! ...........................................................

« previous php.general (#264181) next »