On Wed, 2007-11-07 at 14:53 +0000, Mark Summers wrote: > 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. Given that you expect it to grow then you should have accounted for special characters becoming part of the list. Additionally, you probably think this technique is more efficient since you think that the regex scans the list faster, it does not. If there is nothing to replace the str_replace() call will be faster than the preg_match_all() call since it isn't burdened with the overhead of regex special characters and matching. Additionally, in the event of something actually needing replaced, you will incur a second scan of the target string since preg_match_all() doesn't perform the replacement as soon as it realizes a match is necessary while still having the index of the replacement spot available. In the event that nothing is replaced you can be assured that str_replace() returns a virtual copy of the target string, in which case no duplication overhead is incurred, only the effort to create a COW version of the variable. This is about as fast as the creation of the return value of preg_match_all(). Cheers, Rob. > > 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( > >> "ñ" => "n", > >> "á" => "a", > >> "é" => "e", > >> "í" => "i", > >> "ó" => "o", > >> "ú" => "u" > >> ); > >> > >> $link = "ssrsrsrsóererrereísddósdssú"; > >> > >> 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! ........................................................... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php