For Programmers: Free Programming Magazines  


Home > Archive > PHP Language > May 2004 > can't get preg_replace to work at all









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author can't get preg_replace to work at all
r

2004-05-12, 7:21 pm

Shouldn't this change the contents of $mHr from "Hi" to "Lo"?
It won't work.
I wonder why.

$mHr="Hi";
preg_replace("/Hi/", "/Lo/", $mHr);




Alvaro G Vicario

2004-05-12, 7:21 pm

*** r wrote/escribió (Wed, 12 May 2004 11:17:35 -0500):
> preg_replace("/Hi/", "/Lo/", $mHr);


Try:
preg_replace("/Hi/g", "Lo", $mHr);


--
--
-- Álvaro G. Vicario - Burgos, Spain
--
Andy Hassall

2004-05-12, 7:21 pm

On Wed, 12 May 2004 11:17:35 -0500, "r" <noone@nowhere.com> wrote:

>$mHr="Hi";
> preg_replace("/Hi/", "/Lo/", $mHr);
>
>Shouldn't this change the contents of $mHr from "Hi" to "Lo"?
>It won't work.


It does work. You've just ignored the value returned. It doesn't modify the
variable passed, it returns the string after replacements.

http://uk.php.net/manual/en/function.preg-replace.php

--
Andy Hassall <andy@andyh.co.uk> / Space: disk usage analysis tool
http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space
r

2004-05-12, 7:21 pm

Of course. Thank you!


"Andy Hassall" <andy@andyh.co.uk> wrote in message
news:8er4a05khj2jth0jed9le7587mu8ehij52@
4ax.com...
> On Wed, 12 May 2004 11:17:35 -0500, "r" <noone@nowhere.com> wrote:
>
>
> It does work. You've just ignored the value returned. It doesn't modify

the
> variable passed, it returns the string after replacements.
>
> http://uk.php.net/manual/en/function.preg-replace.php
>
> --
> Andy Hassall <andy@andyh.co.uk> / Space: disk usage analysis tool
> http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space



Andy Hassall

2004-05-12, 7:21 pm

On Wed, 12 May 2004 18:54:13 +0200, Alvaro G Vicario
<alvaro_QUITAR_REMOVE@telecomputeronline.com> wrote:

>*** r wrote/escribió (Wed, 12 May 2004 11:17:35 -0500):
>
>Try:
> preg_replace("/Hi/g", "Lo", $mHr);


The g modifier isn't necessary; unlike the Perl s// operator on which
preg_replace is based, the default is to replace all matches, not just the
first one.

In fact, g isn't even listed in the PCRE manual.

--
Andy Hassall <andy@andyh.co.uk> / Space: disk usage analysis tool
http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com