I'm using file_get_contents on a fairly large file. In it, I need to find every instance of:
http://www.example.com/?foo=1&bar=3
and change it to:
http://www.example.com?foo=1&bar=4
My problem is not understand how preg_replace will replace only a partial match on my regex and not the entire string. For example, pseudocode looks like the following:
$content = file_get_contents($filename);
$pattern = '/http:\/\/www\.example\.com/\?foo=1\&bar=(\d+)';
preg_replace($pattern, "4", $content);
file_put_contents($filename, $content);
I'm almost certain preg_replace($pattern, "4", $content); is wrong in this case. What's the right way to just replace the '3' with the '4'here?
bar=4is not static. it might bebar=10,bar=12341324etc.preg_replacewill change whole pattern to '4', in your case. if you need to change just a one character, you can use @Enissay solution, but be ready, that his solution will change any digits with any length to '4'. If you need replacing exactly that string, you need additional limiting condition.