Re: Regular Expressions

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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.
>   

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux