using eregi_replace to replace only substrings -- not the entire string.
| From: | Kurt Lieber | Date: | Tue, 14 Aug 2001 02:53:01 +0000 |
| Subject: | using eregi_replace to replace only substrings -- not the entire string. | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-62551@lists.php.net to get a copy of this message | ||
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