php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #28755 preg_split doesn't behave as expected for empty tokens
Submitted: 2004-06-12 15:56 UTC Modified: 2004-06-12 16:14 UTC
From: ekitten-listed at comcast dot net Assigned:
Status: Not a bug Package: *Regular Expressions
PHP Version: 4.3.6 OS: Linux
Private report: No CVE-ID: None
 [2004-06-12 15:56 UTC] ekitten-listed at comcast dot net
Description:
------------
If I call preg_split with a string such as ",,foo,,bar,," I expect to get five empty strings back in the returned array, but I only get back two - the first and the last.

Reproduce code:
---------------
$tokens = preg_split("/[,]+/", ",,foo,,bar,,");

Expected result:
----------------
I expect $tokens to contain the following array:

array(12) {
  [0]=>  string(0) ""
  [1]=>  string(0) ""
  [2]=>  string(3) "foo"
  [3]=>  string(0) ""
  [4]=>  string(3) "bar"
  [5]=>  string(0) ""
  [6]=>  string(0) ""
}


Actual result:
--------------
array(12) {
  [0]=>  string(0) ""
  [1]=>  string(3) "foo"
  [2]=>  string(3) "bar"
  [3]=>  string(0) ""
}


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-06-12 16:14 UTC] derick@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

Your scrupt is wrong, [,]+ will also match \",,\" and that will be used to split your script, and not two single \",\"\'s.
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Sun Apr 21 08:01:25 2019 UTC