Req #42129 [Opn->Wfx]: restrict explode's function return type
| From: | cmb@php.net | Date: | Mon, 23 Aug 2021 15:48:32 +0000 |
| Subject: | Req #42129 [Opn->Wfx]: restrict explode's function return type | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-236024@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=42129&edit=1
ID: 42129
Updated by: cmb@php.net
Reported by: david dot nqd at gmail dot com
Summary: restrict explode's function return type
-Status: Open
+Status: Wont fix
Type: Feature/Change Request
-Package: *General Issues
+Package: Strings related
Operating System: Irrelevant
PHP Version: 5.2.3
-Assigned To:
+Assigned To: cmb
Block user comment: N
Private report: N
New Comment:
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.
Previous Comments:
------------------------------------------------------------------------
[2014-12-31 16:24:44] 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.
------------------------------------------------------------------------
[2012-07-19 14:52:43] 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.
------------------------------------------------------------------------
[2007-07-27 22:01:06] 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
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=42129&edit=1