|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #42945 preg_split() swallows part of the string
Submitted: 2007-10-12 12:04 UTC Modified: 2008-01-13 14:44 UTC
Avg. Score:4.0 ± 1.0
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:1 (50.0%)
From: Arne dot Heizmann at csr dot com Assigned:
Status: Closed Package: PCRE related
PHP Version: 5CVS-2007-10-22 OS: *
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: Arne dot Heizmann at csr dot com
New email:
PHP Version: OS:


 [2007-10-12 12:04 UTC] Arne dot Heizmann at csr dot com
The following example code shows how preg_split() - when used with PREG_SPLIT_NO_EMPTY - omits pieces which aren't empty. The returned array SHOULD contain ALL of the characters from the original string minus the characters that match the separator. In my example, the separator is a zero-width match.

I notice that this problem was reported as Bug #15413 before and marked "Bogus". My example shows that the bug is real.

Reproduce code:
    header ('Content-type: text/plain');
    var_export (preg_split ('/\b/', 'a\'', -1, PREG_SPLIT_NO_EMPTY));

Expected result:
array (
  0 => 'a',
  1 => '\'',

Actual result:
array (
  0 => 'a',


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2007-10-22 08:52 UTC]
Verified using latest CVS snapshot.
 [2008-01-12 02:16 UTC]
I tested in PHP 5.3

Index: php_pcre.c
RCS file: /repository/php-src/ext/pcre/php_pcre.c,v
retrieving revision
diff -u -r1. php_pcre.c
--- php_pcre.c  31 Dec 2007 07:17:11 -0000
+++ php_pcre.c  12 Jan 2008 02:14:13 -0000
@@ -1562,7 +1562,7 @@
-       if (!no_empty || start_offset != subject_len)
+       if (!no_empty || last_match[0] != '\0')
                if (offset_capture) {
                        /* Add the last (match, offset) pair to the return value */

 [2008-01-13 14:44 UTC]
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
Thank you for the report, and for helping us make PHP better.

PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Mon Sep 20 11:03:37 2021 UTC