Req #64730 [Opn]: preg_replace_callback vs. preg_replace eval related

From: Date: Mon, 29 Apr 2013 16:49:45 +0000
Subject: Req #64730 [Opn]: preg_replace_callback vs. preg_replace eval related
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-178697@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=64730&edit=1

 ID:                 64730
 Updated by:         laruence@php.net
 Reported by:        imbolk at gmail dot com
 Summary:            preg_replace_callback vs. preg_replace eval related
 Status:             Open
 Type:               Feature/Change Request
 Package:            Regexps related
 Operating System:   Mac OS X 10.8.3
 PHP Version:        5.5.0beta4
 Block user comment: N
 Private report:     N

 New Comment:

a simple patch attached, please also see my proposal: 
http://news.php.net/php.internals/67199


Previous Comments:
------------------------------------------------------------------------
[2013-04-29 16:31:42] imbolk at gmail dot com

Oops… sorry. My mistake. Test script is:

$repl = [
            '/(\d{2}|(?<!\d))([pm])(\d{2}|)([PMc])/e' => function($m) { return 
$this->_op($m[3], $m[4], rtrim($this->_op($m[1], $m[2]), ";"))'; },

            '/(\d{2}|)([MPmplrc])/e' => function ($m) { return $this->_op($m[1], 
$m[2]); },
        ];

$str = preg_replace_callback(array_keys($repl), array_values($repl), $str);

------------------------------------------------------------------------
[2013-04-29 16:30:36] laruence@php.net

The following patch has been added/updated:

Patch Name: sencode_argument.patch
Revision:   1367253036
URL:        https://bugs.php.net/patch-display.php?bug=64730&patch=sencode_argument.patch&revision=1367253036

------------------------------------------------------------------------
[2013-04-29 15:52:38] laruence@php.net

or, add a third argument to callback, which is the "regex self" or the regex 
index?

------------------------------------------------------------------------
[2013-04-28 17:35:06] imbolk at gmail dot com

Description:
------------
In PHP 5.5 'e' key preg_replace is deprecated: 
https://wiki.php.net/rfc/remove_preg_replace_eval_modifier

But I don't know how to replace evaled preg_replace with preg_replace_callback in 
some case.

For example:
$repl = [
            '/(\d{2}|(?<!\d))([pm])(\d{2}|)([PMc])/e' =>
'$this->_op("$3", "$4", 
rtrim($this->_op("$1", "$2"), ";"))',

            '/(\d{2}|)([MPmplrc])/e' => '$this->_op("$1",
"$2")',
        ];

$str = preg_replace(array_keys($repl), array_values($repl), $str);

Test script:
---------------
$repl = [
            '/(\d{2}|(?<!\d))([pm])(\d{2}|)([PMc])/e' => function($m) { return
$this->_op($m[3], $m[4], rtrim($this->_op($m[1], $m[2]), ";"))'; },

            '/(\d{2}|)([MPmplrc])/e' => function ($m) { return $this->_op($m[1],
$m[2]); },
        ];

$str = preg_replace(array_keys($repl), array_values($repl), $str);

Expected result:
----------------
It works.

Actual result:
--------------
Warning: preg_replace_callback(): Requires argument 2, 'Array', to be a valid 
callback in my.php on line 359


------------------------------------------------------------------------



-- 
Edit this bug report at https://bugs.php.net/bug.php?id=64730&edit=1


Thread (11 messages)

« previous php.bugs (#178697) next »