Jump to content
Larry Ullman's Book Forums

Warning: Preg_Replace(): The /e Modifier Is No Longer Supported, Use Preg_Replace_Callback


Recommended Posts

(My setup is in my sig)

 

Here's my code

function decode($source) {

$source = html_entity_decode($source, ENT_QUOTES, "ISO-8859-1");

$source = preg_replace('/(\d+);/me',"chr(\\1)", $source);

$source = preg_replace('/([a-f0-9]+);/mei',"chr(0x\\1)", $source);

return $source;

}

Errors

Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback

 

Revised code

Seems simple enough. so I changed preg_replace with preg_replace_callback

New Errors

Warning: preg_replace_callback(): Requires argument 2, 'chr(\1)', to be a valid callback

Warning: preg_replace_callback(): Requires argument 2, 'chr(0x\1)', to be a valid callback

 

I have been trying to fix this for two days now, and am posting here to find the answer.  Since I'm so new to PHP, the pages on php.net don't make sense to me for this, and the answers that popup on StackOverflow are mostly criticisms of the questions people have posted about them, rather than answering the questions or providing a link to the duplicate question they complain about.

 

My favorite answer was on a website that said to turn off warnings as the solution.  Got a giggle from that one.

 

I can't believe the fix is complicated, but for the life of me, I can't find it.

 

Dale

 

 

 

Link to comment
Share on other sites

Hey! Thanks for the question. My inclination is that there's got to be a simpler way to do whatever you're doing, as PHP has many encoding and decoding functions.

 

Regardless, I imagine the working code would be something like

$source = preg_replace_callback('/(\d+);/me', function($matches) {
  return chr($matches);
}, $source);
Link to comment
Share on other sites

 Share

×
×
  • Create New...