Re: Regular Expressions

From: Date: Wed, 07 Nov 2007 14:53:35 +0000
Subject: Re: Regular Expressions
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-264182@lists.php.net to get a copy of this message
There was no escaping in the original either. Also, I did assume that the list of replacements would grow to become pretty large. I wouldn't suggest doing this for a small list such as that given in the example. Robert Cummings wrote: > 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. >

« previous php.general (#264182) next »