The new php version discards the preg_replace/e modifier.

Source: Internet
Author: User
: This article mainly introduces the new version of php to discard the preg_replacee modifier. if you are interested in the PHP Tutorial, refer to it. Many warnings have been reported when the php version of the server was upgraded to 5.6 recently.

preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead

I didn't pay attention to it at the beginning. later I found many such warnings, so I checked it on the internet and found that php5.5 and above abandoned the modifier/e in the preg_replace function, which means that the replacement rule supports php code during regular expression replacement.

So what should we do?

In fact, you only need to change the code with the/e modifier in preg_replace to preg_replace _ callback and write it again.

Example

Simplest writing method

preg_replace("/([A-Z])/e", "'_' . strtolower('\\1')", $str)

Modify

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. However, it is very troublesome to define a function every time, so we can use anonymous functions.

For example

preg_replace("/([A-Z])/e", "'_' . strtolower('\\1')", $str)

Modify

preg_replace_callback('/([A-Z])/',                      function ($matches) {                        return '_' . strtolower($matches[0]);                      },                      $str)
You can.

Here special warning after modification/([A-Z])/e the last e must be removed or else the error will occur

This can be written in the class.

class a {private $joinStr = "__AAAAA__";public 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 an array instead of a function, indicating that the gwyy method in the $ this class will be automatically accepted by running gwyy.

Next, let's look at an example of a slightly complex point.

$patterns       = '/'.$begin.$parseTag.$n1.'\/(\s*?)'.$end.'/eis';$replacement    = "\$this->parseXmlTag('$tagLib','$tag','$1','')";$content        = preg_replace($patterns, $replacement,$content);

This replacement uses the custom method in the class. if you use an anonymous function to directly set the method, an error will be prompted because the anonymous function context does not have this method and the variable, so use () in addition, you must remove e from the regular expression.
$that = $this;$patterns       = '/'.$begin.$parseTag.$n1.'\/(\s*?)'.$end.'/is';$content=preg_replace_callback($patterns, function($matches) use($tagLib,$tag,$that){    return $that->parseXmlTag($tagLib,$tag,$matches[1],'');},$content);

Use $ thit to replace $ this. now, record it here today.

Xiaoyan original, reprinted please indicate the source!

The above introduces the new version of php to discard the preg_replace/e modifier, including some content, hope to be helpful to friends who are interested in PHP tutorials.

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.