|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #32965 preg_split PREG_SPLIT_DELIM_CAPTURE
Submitted: 2005-05-06 17:05 UTC Modified: 2016-08-28 13:37 UTC
From: elegua at hotmail dot com Assigned: cmb (profile)
Status: Closed Package: PCRE related
PHP Version: 5CVS-2005-05-06 (dev) OS: Sun
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 this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Bug Type:
From: elegua at hotmail dot com
New email:
PHP Version: OS:


 [2005-05-06 17:05 UTC] elegua at hotmail dot com
This is to do with Preg Split. When preg_split is used with the flag PREG_SPLIT_OFFSET_CAPTURE, it returns an array, with the matched string, followed by the offset. 

When the flag PREG_SPLIT_DELIM_CAPTURE is used, it first returns the matched string, and then returns the string.

I am not sure if the functionality of PREG_SPLIT_DELIM_CAPTURE should return as an array, it would be far more useful it did work this way. Is this a bug with my version? Or, is this indeed the way it should function?

Reproduce code:
   foreach($operations as $op) {

Expected result:
each allOps will have an array of string and delim for each match found.

Actual result:
array(allOps[0]=>"some string",allOps[1]="delim")


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2016-08-28 13:37 UTC]
-Status: Open +Status: Closed -Package: Feature/Change Request +Package: PCRE related -Assigned To: +Assigned To: cmb
 [2016-08-28 13:37 UTC]
The behavior is intentional. Returning an array of arrays makes
sense with PREG_SPLIT_OFFSET_CAPTURE, because each substring and
its offset constitute a unit. With PREG_SPLIT_DELIM_CAPTURE,
however, it wouldn't be clear whether the delimiter should belong
to the previous or the following substring; actually, the
delimiter doesn't belong to either.

Furthermore, note that both options may be given to preg_split(),
and it may be useful to know the offset of the delimiter.
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Fri Oct 15 21:03:43 2021 UTC