1

While working with some legacy code, I have came across 2 preg_replace functions with now deprecated /e parameter. PHP suggest to replace it with preg_replace_callback.

These are the functions:

  • First:

    $content = preg_replace("/(\{([a-zA-Z0-9_]+)\})/e", null, $content);
    

    From what I understand, /e is safe to remove from this function?

  • Second:

    $text = preg_replace(
        "/<(h[2])>(.+)<\/(h[2])>/Uie",
        "'<\\1 id=\"'.createIdByText('\\2').'\">'.stripslashes('\\2').'</\\1>'",
        $text
    );
    

Can anyone help me fixing these or converting to preg_replace_callback so they don't throw a deprecation warning?

0

1 Answer 1

2

In the first case you can indeed just remove the e. For the second case:

$text = preg_replace_callback(
    "/<h2>(.+)<\/h2>/Ui",
    function($matches) {
        return '<h2 id="' . createIdByText($matches[1]) . '">' . $matches[1] . '</h2>';
    },
    $text
);

I took the freedom to simplify the regular expression a bit. The stripslashes call is no longer necessary, as it was only there to work around the automatic addslashes call that /e uses.

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

2 Comments

Thank you, I need a few more minutes to test it and will mark it as solved.
p.s. since I can't suggest edit of only 1 character, there is a comma missing after fuinction close bracket. You might want to edit that.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.