Replace Deprecated Preg_Replace /E with Preg_Replace_Callback

Replace deprecated preg_replace /e with preg_replace_callback

You can use an anonymous function to pass the matches to your function:

$result = preg_replace_callback(
"/\{([<>])([a-zA-Z0-9_]*)(\?{0,1})([a-zA-Z0-9_]*)\}(.*)\{\\1\/\\2\}/isU",
function($m) { return CallFunction($m[1], $m[2], $m[3], $m[4], $m[5]); },
$result
);

Apart from being faster, this will also properly handle double quotes in your string. Your current code using /e would convert a double quote " into \".

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

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

$string = preg_replace_callback(
'~�*([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(
'~�*([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

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback in Echelon B3

In this specific case, just remove the /e it does nothing here.

You can also remove the /i
So your code becomes:

function removeColorCode($text) {
return preg_replace('/\^[0-9]/', '', $text);
}

The /e modifier is deprecated, use preg_replace_callback instead of preg_replace()

You can't put the replacement function in fetch_full_ameinfo(), because it needs to refer to the $param1 and $param2 variables, which are local to this function.

This means it needs to use eval() in the current function (this is essentially what preg_replace() does internally when it processes the /e flag).

You need to change the replacement string that fetch_full_ameinfo() creates so that it uses a variable instead of \1, \2, etc. to refer to the capture groups, because the callback function receives the captured matches as an array. So replace the block beginning with if (!$findonly) with this:

if (!$findonly)
{

$ameinfo['find'][] = "~($result[findcode])~i";
$ameinfo['replace'][] = 'ame_match_bbcode($param1, $param2, \'' . $result['ameid'] . '\', \'' . ame_slasher($result['title']) . '\', ' . $result['container'] . ', \'' . ame_slasher($result['replacecode']) . '\', \'$match[1]\', \'$match[2]\', \'$match[3]\', \'$match[4]\', \'$match[5]\', \'$match[6]\')';

}
else
{

$ameinfo['find'][] = "~(\[url\]$result[findcode]\[/url\])~i";
$ameinfo['find'][] = "~(\[url=\"?$result[findcode]\"?\](.*?)\[/url\])~i";
$ameinfo['replace'][] = 'ame_match("$match[1]", "", ' . intval($result['extraction']) .', "' . ($result['embedregexp'] ? "~" . ame_slasher($result['embedregexp']) . "~sim" : "") . '", "' . ($result['validation'] ? "~" . ame_slasher($result['validation']) . "~sim" : "") . '",$ameinfo)';
$ameinfo['replace'][] = 'ame_match("$match[1]", "$match[2]", ' . intval($result['extraction']) .', "' . ($result['embedregexp'] ? "~" . ame_slasher($result['embedregexp']) . "~sim" : "") . '", "' . ($result['validation'] ? "~" . ame_slasher($result['validation']) . "~sim" : "") . '", $ameinfo)';

}

Then change your code to:

    $text = preg_replace_callback($ameinfo['find'], function($match) use (&$param1, &$param2, &$ameinfo) {
return eval($ameinfo['replace']);
}, ($param2 ? $param2 : $param1), 1);

How to convert preg_replace e to preg_replace_callback?

If memory serves, preg_replace_callback() gives you the results of a $match from preg_match() as input, and expects the final result as output. So you'd need to write a function that returns e.g. "code('{$match[1]}')".

It can be an inline function, naturally, if php 5.3 is an option:

preg_replace_callback($regex, function($match) {
// do stuff
return $stuff;
}, $subject);


Related Topics



Leave a reply



Submit