The new php version discards the preg_replace/e modifier.

Source: Internet
Author: User
The new php version discards the preg_replacee modifier. recently, the php version of the server was upgraded to 5.6, and many warnings were reported.

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

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 LastEBe sure to remove it. Otherwise, an error will occur.


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.

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.