[prev in list] [next in list] [prev in thread] [next in thread] 

List:       php-internals
Subject:    Re: [PHP-DEV] [PROPOSAL]Add second to callback of preg_replace_callback
From:       Laruence <laruence () php ! net>
Date:       2013-04-30 2:31:16
Message-ID: CABBJUpdE0mMPCK1KUjPiLjkpiWwOL0U7Bqo8=YrYeneGtx1aJQ () mail ! gmail ! com
[Download RAW message or body]


On Tue, Apr 30, 2013 at 1:43 AM, Nikita Popov <nikita.ppv@gmail.com> wrote:

> On Mon, Apr 29, 2013 at 6:46 PM, Laruence <laruence@php.net> 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);
>
hey:

efficiency & simplicity

do you use foreach even there is array_map?


thanks

> }
>
> Nikita
>



-- 
Laruence  Xinchen Hui
http://www.laruence.com/


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic