PHP new version number obsolete preg_replace/e modifier

Source: Internet
Author: User

Recently, the serverphp version number has been upgraded to 5.6 and a lot of warnings have been found.

Preg_replace (): the/e modifier is deprecated, use preg_replace_callback instead

Didn't notice at the beginning. Later found that a lot of such warnings, so the online search for the php5.5 version number above to discard the Preg_replace function in the/e this modifier is meant to let the regular replacement rules support PHP code

So what should we do?

In fact, you just have to change the code inside the Preg_replace with the/E modifier to preg_replace _callback and then write it down again.


Sample Example


The simplest notation

Preg_replace ("/([A-z])/E", "' _ '. Strtolower (' \\1 ') ", $STR)

Change into

Preg_replace ("/([A-z])/", ' Gwyy ', $str), function Gwyy ($match) {return  ' _ '. Strtolower ($match [1]);}



The second parameter is a function name and then a function is written externally, but we feel very troublesome to define a function every time, so we can use anonymous functions



Say


Preg_replace ("/([A-z])/E", "' _ '. Strtolower (' \\1 ') ", $STR)

Change into

Preg_replace_callback ('/([A-z])/',                      function ($matches) {                        return ' _ '. Strtolower ($matches [0]);                      },                      $str)
'll be


Special warning Here after the change/([A-z])/E last e Be sure to get rid of it or make a mistake.



Suppose you can write that 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 representing the invocation of the Gwyy method inside the $this class GWYY will take the initiative to accept a $match




Here's another example of a slightly more complex point

$patterns       = '/'. $begin. $parseTag. $n 1. ' \ (\s*?) '. $end. ' /eis '; $replacement    = "\ $this->parsexmltag (' $tagLib ', ' $tag ', ' $ ', ')"; $content        = Preg_replace ($ Patterns, $replacement, $content);

This substitution uses its own definition method inside the class. If you use an anonymous function to set the word, you will be prompted with an error because there is no such method and the variable in the anonymous function context, so the use () should be used to introduce the same time attention must be removed from 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);



Here replace $this with $that; All right, let's record it here today.


Small smoke original, reproduced please indicate the source!

PHP new version number obsolete preg_replace/e modifier

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.