php.net |  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 you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: elegua at hotmail dot com
New email:
PHP Version: OS:

 

 [2005-05-06 17:05 UTC] elegua at hotmail dot com
Description:
------------
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:
---------------
$result="";
$operations=preg_split("/(&\d*=op)/",$ops);
   foreach($operations as $op) {
   $allOps[]=$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")

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-08-28 13:37 UTC] cmb@php.net
-Status: Open +Status: Closed -Package: Feature/Change Request +Package: PCRE related -Assigned To: +Assigned To: cmb
 [2016-08-28 13:37 UTC] cmb@php.net
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-2025 The PHP Group
All rights reserved.
Last updated: Thu Jul 03 17:01:33 2025 UTC