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
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
45 + 18 = ?
Subscribe to this entry?

 
 [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

Add a Patch

Pull Requests

Add a Pull Request

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-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 23 13:01:29 2024 UTC