Re: Regular Expressions
| From: | Ezequiel Gutesman | Date: | Tue, 06 Nov 2007 16:44:43 +0000 |
| Subject: | Re: Regular Expressions | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-264123@lists.php.net to get a copy of this message | ||
Maybe this helps
<?php
$line = preg_replace_callback(
'/(á|é|í|ó|ú|ñ)/',
create_function(
// single quotes are essential here,
// or alternative escape all $ as \$
'$matches',
'switch($matches[0]){
case \'á\': return \'a\';
case \'é\': return \'e\';
case \'í\': return \'i\';
case \'ó\': return \'o\';
case \'ú\': return \'u\';
case \'ñ\': return \'n\';
}'
),
$line
);
echo $line;
?>
if you want to use this functionality several times:
<?php
function myReplace($chr)
{
switch($chr[0]){
case 'á': return 'a';
case 'é': return 'e';
case 'í': return 'i';
case 'ó': return 'o';
case 'ú': return 'u';
case 'ñ': return 'n';
}
}
$line = "Hola que tal con á con acento y eñe ";
$line = preg_replace_callback(
'/(á|é|í|ó|ú|ñ)/',
'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('ñ','n',$link);
> $link = ereg_replace('á','a',$link);
> $link = ereg_replace('é','e',$link);
> $link = ereg_replace('í','i',$link);
> $link = ereg_replace('ó','o',$link);
> $link = ereg_replace('ú','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