May I ask questions about preg_replace replacement?

Source: Internet
Author: User
Ask questions about preg_replace replacement string & nbsp; $ str & nbsp; = & nbsp; 'AA _ cc _ ee '; attempt to use such a statement: & nbsp; preg_replace ('/_ {2 }? /', & Nbsp; arr ask about preg_replace replacement
String $ str = 'AA _ cc _ ee ';
An attempt is made to use the following statement: preg_replace ('/_ {2 }? /', Array ('BB', 'DD'), $ str );
Replace with 'aabbccddee'
But all changed to bb. I have added? It indicates that it is not greedy. I hope that the second _ double underline will not be replaced after the first match is replaced, but I find that this is not successful. what should I do? Is my understanding of the greedy model wrong?
Share:
------ Solution --------------------
$ Str = 'AA _ cc _ ee ';
$ A = array ('BB ', 'DD ');
Echo preg_replace ('/_ {2 }? /E', 'Array _ shift ($ a) ', $ str );
Aabbccddee
However, the modifier "e" has been canceled in php5.5, so it is still a forward looking point, so it will not be difficult to change it in the future.
$ Str = 'AA _ cc _ ee ';
$ A = array ('BB ', 'DD ');
Echo preg_replace_callback ('/_ {2 }? /', Function ($ r) use (& $ a) {return array_shift ($ a) ;}, $ str );

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.