On Mon, Apr 29, 2013 at 6:46 PM, Laruence <[email protected]> wrote:
> Hey:
> there comes a FR: https://bugs.php.net/bug.php?id=64730
>
> the main idea is, in 5.5 we remove support of 'e' modifier.
>
> then comes a problem, the old codes(a real use case see
> https://github.com/php/php-src/blob/PHP-5.4/Zend/zend_vm_gen.php#L390):
>
> preg_replace(array(
>
> "/pattern1(.*)/",
> "/pattern2(.*)/"
> ),
> array(
> "/replace1/e",
> "/replace2/e"
> )
> ..),
>
> can not be easier convert to the "callback" style.
>
> then I have to change it to something very ugly like(a real use case
> see: https://github.com/php/php-src/blob/PHP-5.5/Zend/zend_vm_gen.php#L390
> ):
>
> function callback($subject) {
> if (!strncmp($subject, "pattern1", 8)) {
> //function for pattern 1
> } else if(!strncmp($subject, "pattern2", 8)) {
> //function for pattern 2
> } else .....
>
> }
>
> so I propose to add a second argument to callback(aim to php-5.5.1),
> which is the matched regex string self.
>
> then I can simplify the previous codes to:
>
> function callback ($subject, $regex) {
> $replace_funcs = array(
> "/pattern1(.*)" => function ($subect) { //function for
> parttern 1; },
> "/pattern2(.*)" => function($sbuect) { //function for pattern
> 2; }
> );
>
> $replace_funcs[$regex]($subject);
> }
>
> what do you think(of course, the second argument can also easily change
> to be the regex idx in the regex array)?
>
> patch is here:
>
> https://bugs.php.net/patch-display.php?bug_id=64730&patch=sencode_argument.patch&revision=latest
>
>
What's wrong with this?
$replacements = ['regex' => 'callback'];
foreach ($replacements as $regex => $callback) {
$str = preg_replace_callback($regex, $callback, $str);
}
Nikita