JTG
JTG

Reputation: 35

PHP Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead - line 58

I was wondering if someone might be able to help me with this one.

We are receiving this error in our error logs:

[13-Mar-2018 16:42:40 UTC] PHP Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in public_html/check_requirements.php on line 58

Line 58 of check_requirements.php is:

$string = preg_replace('~&#0*([0-9]+);~e', 'chr(\\1)', $string);

I'm afraid we just host the web site for someone else and the error appears to have occurred due to a recent PHP upgrade.

Does anyone know how I can alter line 58 of the code to fix the problem?

Many thanks for your help

James


15/03/2018

Thanks Eydamos. I've replaced the line in the code with your suggestion

$string = preg_replace_callback(
'~&#0*([0-9]+);~',
function ($matches) {
    return chr($matches[1]);
},
$string
);

Unfortunately I then loaded the site again and checked the error log and it came up with the following:

[15-Mar-2018 09:02:09 UTC] PHP Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in public_html/check_requirements.php on line 57

Line 57 was:

$string = preg_replace('~&#x0*([0-9a-f]+);~ei', 'chr(hexdec("\\1"))', $string);

It looks like your first suggestions fixed that issue but then another one has become apparent. I'm not sure what I should do with this one either. Would you mind taking a look for me? If it helps, I can paste the entire code.

I really appreciate your help with this one. It's totally out of my area of expertise - just something that we've inherited from an old site we've taken over the hosting of.

Upvotes: 0

Views: 7237

Answers (2)

Eydamos
Eydamos

Reputation: 601

This is an easy fix you just have to convert the second parameter into a function. Like this:

$string = preg_replace_callback(
    '~&#0*([0-9]+);~',
    function ($matches) {
        return chr($matches[1]);
    },
    $string
);

Beside you can archive the same result way easier:

$string = html_entity_decode($string);

The second one is just as easy as the first one:

$string = preg_replace_callback(
    '~&#x0*([0-9a-f]+);~i',
    function ($matches) {
        return chr(hexdec($matches[1]));
    },
    $string
);

Basically you just have to make three steps:

  1. Remove the character 'e' after the delimiter (in your case the '~' is the delimiter)
  2. Convert the second parameter to a function. You just have to return what was in the second parameter but without quotes so the functions like chr will be called
  3. In the function replace \\1, \\2, ... with $matches[1], $matches[2] and so on

Upvotes: 4

Diab
Diab

Reputation: 127

according to PHP documentation http://php.net/manual/en/reference.pcre.pattern.modifiers.php the 'e' modifier is deprecated from version 5.5.0 and is removed from PHP 7 , so if you are considering upgrade to PHP 7 you should replace your code to something similar to the documentation or an answer like @Eydamos provided

Upvotes: -1

Related Questions