New version of PHP deprecated preg_replace/e modifier

Source: Internet
Author: User
Tags deprecated php tutorial
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.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.