Re: Regular Expressions

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

 



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

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[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