php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #62461 error in explode
Submitted: 2012-07-02 02:16 UTC Modified: 2012-07-08 19:16 UTC
From: php dot net at pravec dot de Assigned:
Status: Not a bug Package: Strings related
PHP Version: 5.3.14 OS: debian
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: php dot net at pravec dot de
New email:
PHP Version: OS:

 

 [2012-07-02 02:16 UTC] php dot net at pravec dot de
Description:
------------
---
From manual page: http://www.php.net/function.explode#refsect1-function.explode-description
---


Test script:
---------------
$a = explode("\r\n", "");
$b = fixed_explode("\r\n", "");

printf("(%d/%d)", count($a), count($b));

function fixed_explode($delimiter, $string, $limit = 0xFFFFFFFF)
	{
	return($string ? explode($delimiter, $string, $limit) : array());
	}


Expected result:
----------------
result will be (1/0) but should be (0/0)

it's on you to fix this. i guess this must be an error.


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-07-02 03:01 UTC] aharvey@php.net
-Status: Open +Status: Not a bug
 [2012-07-02 03:01 UTC] aharvey@php.net
Quoting the manual:

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

This is the second case: the delimiter isn't in the string, so an array 
containing the string is returned.

Not a bug -> closing.
 [2012-07-07 22:46 UTC] php dot net at pravec dot de
i know ... php is not vbscript. but take this

a = ""
For Each b in Split(a, ":")
   Call MsgBox(b)
Next

a can contain "1", "1:2", or ""

if a is "1" a single popup will appear
if a is "1:2" two popup will appear
if a is "" no popup will appear

if string is empty, counting explode have to return 0, not 1

nothing divided by something will return: nothing ... not 1
 [2012-07-07 22:59 UTC] rasmus@php.net
Why should an empty string be treated differently from a non-empty string here? 
explode() is defined to return an array of strings each of which is a substring 
of string formed by splitting it on boundaries formed by the string delimiter. So 
you have string "abc" or you have string "" and you try to split it by "x". "abc" 
split up by the delimiter "x" is just going to give you "abc" just like "" split 
by delimiter "x" is just going to give you ""
 [2012-07-08 19:16 UTC] php dot net at pravec dot de
as i can see, javascript works like php. so asp/vbs is a special case. strange

so i have to do it different way (translating from asp to php)
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 10:01:28 2024 UTC