php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #38912 preg_split('/\s+/', '') returns an array of length one
Submitted: 2006-09-21 14:11 UTC Modified: 2006-09-24 17:46 UTC
From: barry dot wills at providence dot org Assigned:
Status: Not a bug Package: PCRE related
PHP Version: 5.1.6 OS: Red Hat Fedora Core 4
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: barry dot wills at providence dot org
New email:
PHP Version: OS:

 

 [2006-09-21 14:11 UTC] barry dot wills at providence dot org
Description:
------------
preg_split('/\s/', '') is used to split a string whose fields are delimited by whitespace chars. When the string is empty (""), preg_split returns a populated array rather than an empty array.

Expected: the equivalent in Perl returns an empty array (split(/\s/, '')).

Unexpected: preg_split in PHP returns a populated array.

Reproduce code:
---------------
// Example 1 produces: Array ( [0] => )
$a = preg_split('/\s/', '');
print_r($a);

// Example 2 produces: Array ( [0] => )
$a = preg_split('/\s+/', '');
print_r($a);

// Example 3 produces: Array ( [0] => [1] => ) 
$a = preg_split('/\s*/', '');
print_r($a);

// Perl produces an empty array in all three cases.

Expected result:
----------------
No output. Which is to say, the above code should be equivalent to PHP's:

$a = array();

Actual result:
--------------
Array ( [0] => )
Array ( [0] => )
Array ( [0] => [1] => )


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-09-24 17:46 UTC] iliaa@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

To avoid empty matches you need to pass the 
PREG_SPLIT_NO_EMPTY flag to preg_split().
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Jul 16 13:01:31 2025 UTC