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 );