The recent server PHP version has been upgraded to 5.6 and found a lot of warnings .
Preg_replace (): the/e modifier is deprecated, use preg_replace_callback instead
At first did not pay attention, and later found a lot of such warnings, so the online search for the php5.5 version of the above to discard the preg_replace function of the/e this modifier means that the modifier is to let the regular replacement of the rule support PHP code
So what should we do?
In fact, as long as the preg_replace inside has the/E modifier code modified to Preg_replace _callback and then re-write it.
Example
The simplest notation
Preg_replace ("/([A-z])/E", "' _ '. Strtolower (' \\1 ') ", $STR)
Modified into
Preg_replace ("/([A-z])/", ' Gwyy ', $str), function Gwyy ($match) {return ' _ '. Strtolower ($match [1]);}
The second argument is a function name and then writes a function externally, but we feel a lot of trouble defining a function every time, so we can use anonymous functions.
For example
Preg_replace ("/([A-z])/E", "' _ '. Strtolower (' \\1 ') ", $STR)
Modified into
Preg_replace_callback ('/([A-z])/', function ($matches) { return ' _ '. Strtolower ($matches [0]); }, $str)
Can
here is a special warning after the modified/([A-z])/E last e Be sure to get rid of it or make a mistake.
If you can write this in a class,
Class A {Private $JOINSTR = "__aaaaa__";p ublic function __construct () {$this->joinstr = Preg_replace_callback ("/__ ([ a-z_-]+) __/su ", Array ($this, ' Gwyy '), $this->joinstr); Echo $this->joinstr;} Publicfunction Gwyy ($match) {print_r ($match); return ' AAA ';}} $a = new A ();
The second parameter is not a function and becomes an array that calls the Gwyy method inside the $this class to execute GWYY automatically accepts a $match parameter table
Let's look at a slightly more complicated example.
$patterns = '/'. $begin. $parseTag. $n 1. ' \ (\s*?) '. $end. ' /eis '; $replacement = "\ $this->parsexmltag (' $tagLib ', ' $tag ', ' $ ', ')"; $content = Preg_replace ($ Patterns, $replacement, $content);
This substitution uses a custom method inside the class. If you use the anonymous function to set the message directly, you will be prompted with an error because there is no such method and the variable in the anonymous function context, so use () should be used to introduce and note that you must remove the regular e
$that = $this; $patterns = '/'. $begin. $parseTag. $n 1. ' \ (\s*?) '. $end. ' /is '; $content =preg_replace_callback ($patterns, function ($matches) use ($tagLib, $tag, $that) { return $that Parsexmltag ($tagLib, $tag, $matches [1], ');}, $content);
Use $thit instead of $this here; All right, let's record it here today.
Small smoke original, reproduced please indicate the source!
The above describes the new version of PHP deprecated preg_replace/e modifier, including the content, I hope the PHP tutorial interested in a friend helpful.