php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #48704 preg_split with limit issues similar to old explode limit issues.
Submitted: 2009-06-26 14:20 UTC Modified: 2009-06-26 23:16 UTC
From: RQuadling at GoogleMail dot com Assigned:
Status: Not a bug Package: PCRE related
PHP Version: 5.3CVS-2009-06-26 (CVS) OS: Windows XP SP3
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: RQuadling at GoogleMail dot com
New email:
PHP Version: OS:

 

 [2009-06-26 14:20 UTC] 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.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-06-26 14:24 UTC] rquadling@php.net
Missed a small point. If no limit is supplied explode() and preg_split() 
provide the same output (with a valid or invalid delimiter).
 [2009-06-26 18:49 UTC] sjoerd-php at linuxonly dot nl
Thank you for your bug report.

The $limit parameter to explode() and preg_split() has different meanings. They differ when $limit is negative. The behavior of $limit is correctly documented and thus different for both functions.

Bug 48703 suggests than the preg_split() documentation gets improved to describe that the last element of the array contains the remaining part of the string. It does not suggest that the documentation for the limit parameter is changed to be the same as that of explode().

Your example assumes that the $limit parameter behaves the same for preg_split() and explode(), which is not the case. Thus, I don't think your report describes a bug.
 [2009-06-26 23:16 UTC] jani@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 18 12:01:28 2024 UTC