php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #34876 preg_split should return empty array when splitting ""
Submitted: 2005-10-14 22:12 UTC Modified: 2005-10-18 22:14 UTC
From: embeddedlinuxguy at gmail dot com Assigned:
Status: Not a bug Package: Feature/Change Request
PHP Version: 5.0.5 OS: Linux
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: embeddedlinuxguy at gmail dot com
New email:
PHP Version: OS:

 

 [2005-10-14 22:12 UTC] embeddedlinuxguy at gmail dot com
Description:
------------
$b = preg_split("/q/", "", PREG_SPLIT_NO_EMPTY);

should return an empty array (0 elements).  Instead it returns an array with one element which is an empty string, i.e. ("").  This is the same with or without PREG_SPLIT_NO_EMPTY.

Compare to Perl

my @a = split(/q/, "");

Here @a == () not ("")



Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-10-18 22:14 UTC] embeddedlinuxguy at gmail dot com
Oops, I should have used
preg_split("/q/", "", -1, PREG_SPLIT_NO_EMPTY);
which works.

If preg_split is supposed to be Perl Compatible (as in PCRE) then this behavior ought to be changed to match Perl's split() by default:

By default, empty leading fields are preserved, and empty trailing ones are deleted.  (If all fields are empty, they are considered to be trailing.)

Not sure if this is a PHP bug, a PCRE bug, or a feature.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Jan 15 10:01:29 2025 UTC