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?

NikiC
  • 100,734
  • 37
  • 191
  • 225
ek9
  • 3,392
  • 5
  • 23
  • 34

1 Answers1

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.

NikiC
  • 100,734
  • 37
  • 191
  • 225
  • Thank you, I need a few more minutes to test it and will mark it as solved. – ek9 Mar 16 '14 at 16:36
  • 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. – ek9 Mar 16 '14 at 16:52