0

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);

2 Answers 2

4
$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.

Sign up to request clarification or add additional context in comments.

2 Comments

@BhumiShah use (?<=interval=["'])\d+ for that
@BhumiShah $re = "/(?<=interval=[\"'])\\d+/m";
0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.