1

I just upgraded to PHP7 and got this warning :

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

How do I replace preg_replace (the second preg_replace in below code) to preg_replace_callback? It's a 3rd party code which I don't really understand. I know some PHP and have tried to look for the solution, but I have trouble understanding it.

// Remove HTML Entities //
$string = preg_replace('/&(#?x?[0-9a-z]+)?;/', '', $string); 

$search = array ("'<script[^>]*?>.*?</script>'si",  // Strip out javascript
             "'<[\/\!]*?[^<>]*?>'si",           // Strip out HTML tags
             "'([\r\n])[\s]+'",                 // Strip out white space
             "'&(quot|#34);'i",                 // Replace HTML entities
             "'&(amp|#38);'i",
             "'&(lt|#60);'i",
             "'&(gt|#62);'i",
             "'&(nbsp|#160);'i",
             "'&(iexcl|#161);'i",
             "'&(cent|#162);'i",
             "'&(pound|#163);'i",
             "'&(copy|#169);'i",
             "'&#(\d+);'e");        

if ($search && $string){
    return preg_replace($search, ' ', $string);        // Line 322, the line that I need to fix
} else {
    return false;
}
2
  • 1
    the e flag doesn't do anything in that case, just remove it. Commented Jan 18, 2017 at 10:07
  • Whatever, it seems to be a poorly written code: (Why removing entities? Why not all entities? Why tags are not removed using DOMDocument or strip_tags. Why removing indentation except for the first line?). Try to understand the goal of this piece of code and rewrite it entirely in a better way. Commented Jan 18, 2017 at 11:37

1 Answer 1

1

The /e modifier evaluates PHP code in the replacement string.

Because your code replaces the found strings only with a whitespace, the modifier has no use at all and you can simply remove the e in the line "'&#(\d+);'e");.

You can find more information on this page: PHP Pattern Modifiers

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.