multiple test (regexp)

multiple test (regexp)

am 12.09.2007 20:54:42 von pepper.gabriela

Hello, I use this code:


$a =3D "&&&&&& a b c d e f g";
$b =3D ereg_replace ("&", "%26", $a);

echo $b;

to subtitute every "&" occurrence in $a.

What if I want to change other occurrences of other chars at the same
time?
Say, I want to substitute "?", "*" and "°"...

How can I do?

Re: multiple test (regexp)

am 12.09.2007 21:42:05 von Macca

Maybe you should look up the built in function urlencode

http://uk.php.net/manual/en/function.urlencode.php

Re: multiple test (regexp)

am 12.09.2007 21:47:09 von pepper.gabriela

> http://uk.php.net/manual/en/function.urlencode.php


Sorry for my example... but I need exactly what I asked, not url-
encoding :-)

Re: multiple test (regexp)

am 12.09.2007 21:52:41 von gosha bine

pepper.gabriela@gmail.com wrote:
> Hello, I use this code:
>
>
> $a = "&&&&&& a b c d e f g";
> $b = ereg_replace ("&", "%26", $a);
>
> echo $b;
>
> to subtitute every "&" occurrence in $a.
>
> What if I want to change other occurrences of other chars at the same
> time?
> Say, I want to substitute "?", "*" and "°"...
>
> How can I do?
>

hi

it's not clear from your post what exactly you're after

maybe strtr function helps you

$trans = array(
'&' => "%26",
'?' => "%3F",
'%' => "%25",
);

echo strtr("a & b ? c % d", $trans);


--
gosha bine

extended php parser ~ http://code.google.com/p/pihipi
blok ~ http://www.tagarga.com/blok

Re: multiple test (regexp)

am 12.09.2007 22:00:58 von zeldorblat

On Sep 12, 2:54 pm, pepper.gabri...@gmail.com wrote:
> Hello, I use this code:
>
> $a =3D "&&&&&& a b c d e f g";
> $b =3D ereg_replace ("&", "%26", $a);
>
> echo $b;
>
> to subtitute every "&" occurrence in $a.
>
> What if I want to change other occurrences of other chars at the same
> time?
> Say, I want to substitute "?", "*" and "°"...
>
> How can I do?

No regex needed -- just good old str_replace():

$b =3D str_replace(array('&', '?', '*'), '%26', $a);

Re: multiple test (regexp)

am 14.09.2007 10:19:43 von pepper.gabriela

Thank you all!