Hey:
   Sorry for the delay,  the new patch, which make the second argument the
regex array keys is attached.


https://bugs.php.net/patch-display.php?bug_id=64730&patch=second_arg_rege_key.patch&revision=latest

   with this patch,  preg_replace_callback will call user callback with two
arguments, the first one is the same,  the second is the regex key if the
regex is an array or NULL if the regex is a string.

   then we can do something like:

    $code = preg_replace_callback(
array(
"foo" => "/some very complex regex/",
"bar" => "/another one/",
...
),
function($matches, $idx)  {
 switch ($idx) {
  case 'foo'
    ...
  case 'bar':
    ...
 }
},
$code);


  if no objections,  I will commit this patch after 5.5. 0 final release..

  thanks


On Tue, Apr 30, 2013 at 12:46 AM, Laruence <larue...@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
>
> --
> Laruence  Xinchen Hui
> http://www.laruence.com/
>



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

Reply via email to