ID:               48704
 Updated by:       rquadl...@php.net
 Reported By:      RQuadling at GoogleMail dot com
 Status:           Open
 Bug Type:         PCRE related
 Operating System: Windows XP SP3
 PHP Version:      5.3CVS-2009-06-26 (CVS)
 New Comment:

Missed a small point. If no limit is supplied explode() and
preg_split() 
provide the same output (with a valid or invalid delimiter).


Previous Comments:
------------------------------------------------------------------------

[2009-06-26 14:20:48] RQuadling at GoogleMail dot com

Description:
------------
This comes from the recent opened doc bug
(http://bugs.php.net/bug.php?
id=48703) and fixed bug http://bugs.php.net/bug.php?id=47560.

In #48703, the OP says that the limit on preg_split() should be 
documented the same as the limit in explode (split).

But the following code shows that the output is different in certain 
circumstances.

For #48703 to be closed with the documentation being updated to match 
that of explode, then preg_split() needs to be amended to match the 
behaviour of explode with regards to the limit parameter.




Reproduce code:
---------------
<?php
$string = 'one.two.three.four';

foreach(array('Valid' => '.', 'Invalid' => '_') as $valid =>
$delimiter) {
        $results[$valid]['No Limit'] = explode($delimiter, $string);

        foreach(range(-4, 4) as $limit) {
                $explode    = explode($delimiter, $string, $limit);
                $preg_split = preg_split("`\\{$delimiter}`", $string, $limit);
                if (serialize($explode) !== serialize($preg_split)) {
                        echo
                                "BAD        : Output does not match for a 
delimiter of '$delimiter'
($valid) and a limit of $limit.", PHP_EOL,
                                'EXPLODE    : ', implode(' : ', $explode), 
PHP_EOL,
                                'PREG_SPLIT : ', implode(' : ', $preg_split), 
PHP_EOL, PHP_EOL;
                } else {
                        echo "GOOD       : Output does match for a delimiter of 
'$delimiter'
($valid) and a limit of $limit.", PHP_EOL, PHP_EOL;
                }
        }
}


Expected result:
----------------
GOOD : Output does not match for a delimiter of '.' (Valid) and a 
limit of -4.

GOOD : Output does not match for a delimiter of '.' (Valid) and a 
limit of -3.

GOOD : Output does not match for a delimiter of '.' (Valid) and a 
limit of -2.

GOOD : Output does not match for a delimiter of '.' (Valid) and a 
limit of -1.

GOOD : Output does not match for a delimiter of '.' (Valid) and a 
limit of 0.

GOOD : Output does not match for a delimiter of '.' (Valid) and a 
limit of 1.

GOOD : Output does not match for a delimiter of '.' (Valid) and a 
limit of 2.

GOOD : Output does not match for a delimiter of '.' (Valid) and a 
limit of 3.

GOOD : Output does not match for a delimiter of '.' (Valid) and a 
limit of 4.

GOOD : Output does not match for a delimiter of '_' (Invalid) and a 
limit of -4.

GOOD : Output does not match for a delimiter of '_' (Invalid) and a 
limit of -3.

GOOD : Output does not match for a delimiter of '_' (Invalid) and a 
limit of -2.

GOOD : Output does not match for a delimiter of '_' (Invalid) and a 
limit of -1.

GOOD : Output does not match for a delimiter of '_' (Invalid) and a 
limit of 0.

GOOD : Output does not match for a delimiter of '_' (Invalid) and a 
limit of 1.

GOOD : Output does not match for a delimiter of '_' (Invalid) and a 
limit of 2.

GOOD : Output does not match for a delimiter of '_' (Invalid) and a 
limit of 3.

GOOD : Output does not match for a delimiter of '_' (Invalid) and a 
limit of 4.

Actual result:
--------------
BAD        : Output does not match for a delimiter of '.' (Valid) and 
a limit of -4.
EXPLODE    :
PREG_SPLIT : one.two.three.four

BAD        : Output does not match for a delimiter of '.' (Valid) and 
a limit of -3.
EXPLODE    : one
PREG_SPLIT : one.two.three.four

BAD        : Output does not match for a delimiter of '.' (Valid) and 
a limit of -2.
EXPLODE    : one : two
PREG_SPLIT : one.two.three.four

BAD        : Output does not match for a delimiter of '.' (Valid) and 
a limit of -1.
EXPLODE    : one : two : three
PREG_SPLIT : one : two : three : four

BAD        : Output does not match for a delimiter of '.' (Valid) and 
a limit of 0.
EXPLODE    : one.two.three.four
PREG_SPLIT : one : two : three : four

GOOD       : Output does match for a delimiter of '.' (Valid) and a 
limit of 1.

GOOD       : Output does match for a delimiter of '.' (Valid) and a 
limit of 2.

GOOD       : Output does match for a delimiter of '.' (Valid) and a 
limit of 3.

GOOD       : Output does match for a delimiter of '.' (Valid) and a 
limit of 4.

BAD        : Output does not match for a delimiter of '_' (Invalid) 
and a limit of -4.
EXPLODE    :
PREG_SPLIT : one.two.three.four

BAD        : Output does not match for a delimiter of '_' (Invalid) 
and a limit of -3.
EXPLODE    :
PREG_SPLIT : one.two.three.four

BAD        : Output does not match for a delimiter of '_' (Invalid) 
and a limit of -2.
EXPLODE    :
PREG_SPLIT : one.two.three.four

BAD        : Output does not match for a delimiter of '_' (Invalid) 
and a limit of -1.
EXPLODE    :
PREG_SPLIT : one.two.three.four

GOOD       : Output does match for a delimiter of '_' (Invalid) and a 
limit of 0.

GOOD       : Output does match for a delimiter of '_' (Invalid) and a 
limit of 1.

GOOD       : Output does match for a delimiter of '_' (Invalid) and a 
limit of 2.

GOOD       : Output does match for a delimiter of '_' (Invalid) and a 
limit of 3.

GOOD       : Output does match for a delimiter of '_' (Invalid) and a 
limit of 4.



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


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

Reply via email to