Reputation: 9476
I have below string
$string = 'id="0000001" did="659" interval="2" media.jpg'
I want to replace 2 (value is dynamic 0 to 1000) in interval tag with 0. How can I do it?
I tried following code but it replaces all
$returnValue = preg_replace('/\\d+/', '0', $string, -1, $count);
Upvotes: 0
Views: 78
Reputation: 405
This is an another way to do this without using lookbehind assertion however using lookbehind is much cooler.
preg_replace('/(interval=")(\d+)/','${1}0', $string, -1, $count);
Upvotes: 0
Reputation: 67968
$returnValue = preg_replace('/(?<=interval=")\\d+/', '0', $string, -1, $count);
Just include a lookbehind
stating interval="
should be present before the \\d+
you are trying to find.
Upvotes: 4