Replace the first regular expression with 0 in PHP. Ignored

Source: Internet
Author: User
{Code ...}
$ Subject = 'your-region c-ec5b-9d8c-2caa0b8fe45d '; echo preg_replace ('/^ (\ w {8}) \ w {2}/I ',' $1 '. '08 ', $ subject); // output result: Success // expected result: Success // But: preg_replace ('regex',' $1 '. '_', $ subject); no problem.

Reply content:
$ Subject = 'your-region c-ec5b-9d8c-2caa0b8fe45d '; echo preg_replace ('/^ (\ w {8}) \ w {2}/I ',' $1 '. '08 ', $ subject); // output result: Success // expected result: Success // But: preg_replace ('regex',' $1 '. '_', $ subject); no problem.

Description of the Back Reference of the PHP manual preg_replace function: preg_replace

When it works in the replacement mode and the back reference is followed by another number (for example, adding an original number after a matching mode ), you cannot use a syntax like \ 1 to describe backward references. For example, \ 11 will make preg_replace () unable to understand that what you want is a \ 1 post-reference followed by an original article 1, or a \ 11 Post-reference followed by nothing. In this case, the solution is \ $ {1} 1. This creates an independent $1 back reference and an independent original 1.

Your replacement part is "$108", so here you need to replace $1 with $ {1} to avoid confusion.


  

echo preg_replace_callback('/^(\w{8})(\w{2})/i',create_function(        // single quotes are essential here,        // or alternative escape all $ as \$        '$matches',        'return $matches[1].\'08\';'    ),$subject);

In this way, callback Processing

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.