#38912 [Opn->Bgs]: preg_split('/\s+/', '') returns an array of length one
| From: | iliaa@php.net | Date: | Sun, 24 Sep 2006 17:46:24 +0000 |
| Subject: | #38912 [Opn->Bgs]: preg_split('/\s+/', '') returns an array of length one | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-102596@lists.php.net to get a copy of this message | ||
ID: 38912
Updated by: iliaa@php.net
Reported By: barry dot wills at providence dot org
-Status: Open
+Status: Bogus
Bug Type: PCRE related
Operating System: Red Hat Fedora Core 4
PHP Version: 5.1.6
New Comment:
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().
Previous Comments:
------------------------------------------------------------------------
[2006-09-21 14:11:31] 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] => )
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=38912&edit=1