1

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;
MTVS
  • 2,046
  • 5
  • 26
  • 37

1 Answers1

3

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
 );
tmuguet
  • 1,165
  • 6
  • 10