Req #70738 [Opn->Wfx]: preg_replace_callback_array doesn't globally respect the $limit argument

From: Date: Mon, 18 Mar 2019 14:58:56 +0000
Subject: Req #70738 [Opn->Wfx]: preg_replace_callback_array doesn't globally respect the $limit argument
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-220050@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=70738&edit=1

 ID:                 70738
 Updated by:         nikic@php.net
 Reported by:        nicolas dot grekas+php at gmail dot com
 Summary:            preg_replace_callback_array doesn't globally respect
                     the $limit argument
-Status:             Open
+Status:             Wont fix
 Type:               Feature/Change Request
 Package:            PCRE related
 PHP Version:        7.0.0RC5
 Block user comment: N
 Private report:     N

 New Comment:

This behavior is consistent with preg_replace_callback():

echo preg_replace_callback(
    array('/[^a]/', '/a/'),
    function () {return 'a';},
    'abca',
    3,
    $count
), "\n";
echo $count, "\n"; // 5

And for that matter also with preg_replace():

echo preg_replace(
    array('/[^a]/', '/a/'),
    'a',
    'abca',
    3,
    $count
), "\n";
echo $count, "\n"; // 5

Regardless of how this ought to have worked originally, I don't think it makes sense to change
this at this point. The limit is for each replacement, rather than for all replacements together.


Previous Comments:
------------------------------------------------------------------------
[2015-10-19 11:31:44] nicolas dot grekas+php at gmail dot com

Description:
------------
preg_replace_callback_array's $limit argument is used independently for all items in the
$patterns array.

I'm not sure this is the behavior that it should have. It makes it possible for example to has
$count > $limit. This is counter intuitive.

Test script:
---------------
echo preg_replace_callback_array(
    array(
        '/[^a]/' => function () {return 'a';},
        '/a/' => function () {return 'd';},
    ),
    'abca',
    3,
    $count
);

echo $count;

Expected result:
----------------
daaa3

Actual result:
--------------
ddda5


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



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


Thread (2 messages)

« previous php.bugs (#220050) next »