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;

1 Answer 1

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
 );
Sign up to request clarification or add additional context in comments.

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.