-1

Need help to solve error like below,

preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in line 601

Got error in below code,

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

AM tried like.

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

But still got error like these?

Requires argument 2, 'chr(hexdec("\1"))'
  • Possible duplicate of [Replace preg\_replace() e modifier with preg\_replace\_callback](http://stackoverflow.com/questions/15454220/replace-preg-replace-e-modifier-with-preg-replace-callback) – Wiktor Stribiżew Apr 05 '17 at 12:00
  • Am tired but not working. – Geetha Janarthanan Apr 05 '17 at 12:01
  • Where is the code where you *tried that*? Where have you tried to pass the anonymous function as the second argument? – Wiktor Stribiżew Apr 05 '17 at 12:03
  • Don't modify [framework files](https://github.com/bcit-ci/CodeIgniter/blob/v2.0.3/system/core/Security.php#L471) by hand; just update it to a recent version. – Narf Apr 05 '17 at 12:09
  • Please check my above code got error like Requires argument 2, 'chr(hexdec("\1"))' – Geetha Janarthanan Apr 05 '17 at 12:17
  • According to Narf's link, your code belongs to an alternative to `html_entity_decode()` written to workaround a PHP/4 bug. Unless you're still using PHP/4 (with is more than dated) there's no reason to avoid the native function. – Álvaro González Apr 05 '17 at 12:20

1 Answers1

2

As the error indicates, the e modifier is no longer supported in your PHP version.

The preg_replace_callback equivalent would look like this:

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

NB: The 0* in your regex is not needed, as zeroes are captured by the pattern that follows that, and it does not bother to have those zeroes captured in the capture group.

BUT, as you are on a PHP version equal or higher than 5.5 (as those versions produce the error), you can rely on html_entity_decode:

$string = html_entity_decode($string);
trincot
  • 317,000
  • 35
  • 244
  • 286