Re: Regular Expressions

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

 



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;

?>

Alberto García Gómez wrote:
> I'm a mess in regular expressions and I make this code:
>
> $link = ereg_replace('&ntilde;','n',$link); 
> $link = ereg_replace('&aacute;','a',$link);
> $link = ereg_replace('&eacute;','e',$link); 
> $link = ereg_replace('&iacute;','i',$link);
> $link = ereg_replace('&oacute;','o',$link); 
> $link = ereg_replace('&uacute;','u',$link);    
>
> I ask if is a way to make those lines into a single one but working as well as this piece. I'm thinking in increase those lines so will be wonderful if I can optimize the code.
>
>
>
> Este correo ha sido enviado desde el Politécnico de Informática "Carlos Marx" de Matanzas.
> "La gran batalla se librará en el campo de las ideas"
>
>   

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