php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #42129 restrict explode's function return type
Submitted: 2007-07-27 22:01 UTC Modified: 2021-08-23 15:48 UTC
Votes:3
Avg. Score:4.3 ± 0.9
Reproduced:2 of 2 (100.0%)
Same Version:0 (0.0%)
Same OS:1 (50.0%)
From: david dot nqd at gmail dot com Assigned: cmb (profile)
Status: Wont fix Package: Strings related
PHP Version: 5.2.3 OS: Irrelevant
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: david dot nqd at gmail dot com
New email:
PHP Version: OS:

 

 [2007-07-27 22:01 UTC] david dot nqd at gmail dot com
Description:
------------
This is not a bug report like #38337.

Shouldn't the return type of explode be restricted to an array given valid inputs (strings)?
This would mean that if an empty string was passed as the delimiter, the return would be an array containing individual characters.

I doubt this would break any existing code unless people used explode to check that the delimiter === ''

Reproduce code:
---------------
<?php
$x = explode('', 'abc');
?>

Expected result:
----------------
$x = array('a','b','c')

Actual result:
--------------
$x = false

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-07-19 14:52 UTC] alastair at propcom dot co dot uk
This affects me too.

I don't want this to be my string-split paradigm:

if ($delimiter === '') return str_split($str);
else return explode($delimiter, $str);

That's just stupid. Splitting on the empty string is a completely logical and 
well-defined operation.
 [2014-12-31 16:24 UTC] nikic@php.net
-Package: Feature/Change Request +Package: *General Issues
 [2014-12-31 16:24 UTC] nikic@php.net
Note that if explode('', 'abc') were implemented the correct result would actually be array('', 'a', 'b', 'c', ''), which would be unexpected to many people. To avoid confusion PHP does not allow this usage.
 [2021-08-23 15:48 UTC] cmb@php.net
-Status: Open +Status: Wont fix -Package: *General Issues +Package: Strings related -Assigned To: +Assigned To: cmb
 [2021-08-23 15:48 UTC] cmb@php.net
As of PHP 8.0.0, a ValueError is thrown if $separator is an empty
string, so in a sense, this feature has been implemented.

Kidding aside, for the reason mentioned by @nikic, an empty
string as separator will not be implemented.  Just use str_split()
instead.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Dec 03 08:01:28 2024 UTC