Re: using eregi_replace to replace only substrings -- not the entire string.
| From: | David Robley | Date: | Tue, 14 Aug 2001 03:00:25 +0000 |
| Subject: | Re: using eregi_replace to replace only substrings -- not the entire string. | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-62552@lists.php.net to get a copy of this message | ||
On Tue, 14 Aug 2001 12:23, Kurt Lieber wrote:
> I'm trying to update a number in a text file using eregi_replace.
>
> The original line is:
>
> $registrationLimit = 100;
>
> and I'd like to change it to:
>
> $registrationLimit = 300;
>
> now, 100 appears multiple times throughout the file, so I have to match
> on the whole string, not just 100. I used the following code: (this
> may wrap in your mail client)
>
> $contents = eregi_replace("registrationLimit =
> ([0-9]+);","registrationLimit = " . $form_registrationLimit .
> ";",$contents);
>
> which works, but I'd like to find a more elegant way to do this, rather
> than rebuilding the entire string. I tried:
>
> $contents = eregi_replace("registrationLimit =
> ([0-9]+);",\\1$form_registrationLimit,$contents);
>
> but that only produces:
>
> $100300
>
> (in other words, it combines the two values and strips out the
> "registrationLimit = " part...) After reading the manual, I understand
> why, but I'm hoping there's some similar such method where I can
> parenthesize a substring in the string pattern and then replace only
> that substring with my replacement string.
>
> Any thoughts?
>
> --kurt
That sounds like a task for str_replace()
--
David Robley Techno-JoaT, Web Maintainer, Mail List Admin, etc
CENTRE FOR INJURY STUDIES Flinders University, SOUTH AUSTRALIA
Windows is the best GUI - It always sticks!