Re: Regular Expressions

From: Date: Wed, 07 Nov 2007 15:10:24 +0000
Subject: Re: Regular Expressions
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-264184@lists.php.net to get a copy of this message
This is a much better solution. I didn't realise that str_replace() accepted arrays as arguments for the search and replace terms. Mental note: reading mailing lists backwards doesn't work. Robert Cummings wrote: > On Tue, 2007-11-06 at 23:24 -0300, Martin Alterisio wrote: > >> 2007/11/6, Alberto García Gómez <alberto@ipimtzcm.rimed.cu>: >> >>> 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. >>> > > <?php > > $map = array > ( > '&ntilde;', 'n', > '&aacute;', 'a', > '&eacute;', 'e', > '&iacute;', 'i', > '&oacute;', 'o', > '&uacute;', 'u', > ); > > $link = > str_replace( > array_keys( $map ), array_values( $map ), $link ); > > ?> > > The only way to make it faster is to build the key array and value array > separately, but then the association is not so clear. > > Cheers, > Rob. >

« previous php.general (#264184) next »