php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #48035 explode() returns empty array if delimiter not found and limit is negative
Submitted: 2009-04-21 11:19 UTC Modified: 2009-04-21 14:05 UTC
Votes:1
Avg. Score:2.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: Bjorn dot Wiberg at its dot uu dot se Assigned:
Status: Closed Package: Strings related
PHP Version: 5.2.9 OS: Debian 5.0.1, IBM AIX 5.3
Private report: No CVE-ID: None
 [2009-04-21 11:19 UTC] Bjorn dot Wiberg at its dot uu dot se
Description:
------------
It appears that explode() returns an empty array (instead of an array with string in it) if limit is negative and delimiter contains a value that is not contained in string.

The documentation of explode() (http://www.php.net/manual/en/function.explode.php, section "Return Values") however states that "If delimiter contains a value that is not contained in string, then explode() will return an array containing string", which then does not hold for negative limits.

Reproduce code:
---------------
<?php 
  print_r(explode('_', 'test', -1));
  print_r(explode('_', 'test_underscore', -1));

  print_r(explode('_', 'test'));
  print_r(explode('_', 'test_underscore'));
?>

Expected result:
----------------
Array
(
    [0] => test
)
Array
(
    [0] => test
)
Array
(
    [0] => test
)
Array
(
    [0] => test
    [1] => underscore
)

Actual result:
--------------
bwiberg@musica-lp02:~$ php -n -f test.php 
Array
(
)
Array
(
    [0] => test
)
Array
(
    [0] => test
)
Array
(
    [0] => test
    [1] => underscore
)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-04-21 12:21 UTC] iliaa@php.net
.
 [2009-04-21 14:00 UTC] rquadling@php.net
A negative limit reduces the size of the resultant array.

So explode('_', 'test') returns a single entry.

A limit of -1 essentially throws that entry away.

The documentation may be better if it said :

"If delimiter contains a value that is not contained in string and a 
negative limit is used, then an empty array will be returned. For any 
other limit, an array containing string will be returned."



 [2009-04-21 14:05 UTC] rquadling@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.


 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Fri Sep 17 08:03:36 2021 UTC