|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #40107 preg_grep should capture subpatterns
Submitted: 2007-01-12 00:28 UTC Modified: 2019-03-18 16:24 UTC
Avg. Score:5.0 ± 0.0
Reproduced:3 of 3 (100.0%)
Same Version:1 (33.3%)
Same OS:0 (0.0%)
From: mattsch at gmail dot com Assigned:
Status: Wont fix Package: PCRE related
PHP Version: 5.2.0 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: mattsch at gmail dot com
New email:
PHP Version: OS:


 [2007-01-12 00:28 UTC] mattsch at gmail dot com
preg_grep should capture subpatterns.  Currently it doesn't have the ability to do this.  

Reproduce code:
array preg_grep ( string pattern, array input [, int flags] )

Expected result:
The return array would return a two dimensional array if you set a flag like PREG_GREP_CAPTURE.  $array[n][0] would be the fully captured string and each additional index in that dimension would return the subpattern matches.


$arrInput = array('5741234-5671','5741235','5741236-432');
$arrMatches = preg_grep('/([2-9][0-9]{6,15})(?:\-([0-9]{2,6}))?/', $arrInput, PREG_GREP_CAPTURE);

Content of $arrMatches:

$arrMatches[0][0] = '5741234-5671'
$arrMatches[0][1] = '5741234'
$arrMatches[0][2] = '5671'
$arrMatches[1][0] = '5741235'
$arrMatches[2][0] = '5741236-432'
$arrMatches[2][1] = '5741236'
$arrMatches[2][2] = '432'

Actual result:
array preg_grep ( string pattern, array input [, int flags] )


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2016-12-30 23:54 UTC]
-Package: Feature/Change Request +Package: PCRE related
 [2019-03-18 16:24 UTC]
-Status: Open +Status: Wont fix
 [2019-03-18 16:24 UTC]
Marking this as won't fix. preg_grep() is a simple filtering functionality -- to capture subpatterns, you can use preg_match() inside a foreach loop.
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Sat Oct 31 14:01:24 2020 UTC