[PROPOSAL]Add second to callback of preg_replace_callback
| From: | Laruence | Date: | Mon, 29 Apr 2013 16:46:03 +0000 |
| Subject: | [PROPOSAL]Add second to callback of preg_replace_callback | ||
| Groups: | php.internals | ||
| Request: | Send a blank email to internals+get-67199@lists.php.net to get a copy of this message | ||
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/