Reputation: 2096
In the following snippet
Why doesn't bar replace foo?
$subject = "Hello foo";
preg_replace_callback(
'/\bfoo\b/i',
function ($match)
{
return 'bar';
},
$subject
);
echo $subject;
Upvotes: 1
Views: 922
Reputation: 1165
preg_replace_callback
does not modify $subject
but returns the new string:
The following code should work:
$subject = "Hello foo";
echo preg_replace_callback(
'/\bfoo\b/i',
function ($match)
{
return 'bar';
},
$subject
);
Upvotes: 3