How can I restrict preg_replace_callback so that it replaces a limited number of expression matches?

33 Views Asked by At

An example of what I need:

$input = "bar bar bar bar bar";
$result = preg_replace_callback('bar', 'foo', $input); // And there must be some way to limit preg_replace_callback so that it replaces only the first 2 (for example) matches of the expression
echo $result;

Result: foo foo bar bar bar

Tried google it but didn't find anything. Sorry, I'm new to php.

0

There are 0 best solutions below