View Full Version : help with preg_replace...

05-17-2011, 04:15 PM

I'm having trouble with preg_replace...
basically i wanted to use str_replace but needed to limit the replacement to 1 so had to resort to preg_replace.

problem is i cant get the thing to work..

for ($i=0; $i<4; $i++) {
$str = preg_replace(${'old_'.$i}, ${'new_'.$i}, $str, 1);

the problem i get is that as soon as the replace occurs, it makes $str empty instead of just replacing what its supposed to.

it all works fine with str_replace but like i said, i needed to limit the replace to 1.


05-17-2011, 05:33 PM
Since you are now using preg_replace(), are the values in ${'old_'.$i} formatted as regular expressions?