Re: Regular Expressions

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

 



Maybe this helps

<?php


 $line = preg_replace_callback(
        '/(&aacute;|&eacute;|&iacute;|&oacute;|&uacute;|&ntilde;)/',
        create_function(
            // single quotes are essential here,
            // or alternative escape all $ as \$
            '$matches',
	    'switch($matches[0]){
		    case \'&aacute;\': return \'a\';
		    case \'&eacute;\': return \'e\';
		    case \'&iacute;\': return \'i\';
		    case \'&oacute;\': return \'o\';
		    case \'&uacute;\': return \'u\';
		    case \'&ntilde;\': return \'n\';
		}'
        ),
        $line
    );

echo $line;
?>

if you want to use this functionality several times:

<?php

function myReplace($chr)
{
	switch($chr[0]){
		    case '&aacute;': return 'a';
		    case '&eacute;': return 'e';
		    case '&iacute;': return 'i';
		    case '&oacute;': return 'o';
		    case '&uacute;': return 'u';
		    case '&ntilde;': return 'n';
	}
}


$line = "Hola que tal con &aacute; con acento y e&ntilde;e ";

 $line = preg_replace_callback(
        '/(&aacute;|&eacute;|&iacute;|&oacute;|&uacute;|&ntilde;)/',
	'myReplace',
        $line
    );
echo $line;
?>

hope this helps. Note that these are pcre (Perl Compatible RegEx).



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

-- 
Ezequiel Gutesman
Researcher
Corelabs
Core Security Technologies
http://www.coresecurity.com/corelabs

PGP Figerprint: 01E4 0E4F 83F8 2D5D 8050 0449 7156 1DF6 C2B3 34AE

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