php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #68529 Prevent double results when using named capture groups
Submitted: 2014-11-30 21:03 UTC Modified: 2016-08-20 10:28 UTC
Votes:2
Avg. Score:4.5 ± 0.5
Reproduced:2 of 2 (100.0%)
Same Version:2 (100.0%)
Same OS:2 (100.0%)
From: gooh@php.net Assigned:
Status: Open Package: PCRE related
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2014-11-30 21:03 UTC] gooh@php.net
Description:
------------
When using preg_match_all with named capture groups, the result array will contain one entry with a numeric key in addition to one entry with the name of the group. Assuming I put the named capture group there on purpose, there is no reason why I would need the numeric index. It only bloats the result array.

As a user I would like to have an option that returns the results without the superfluous numeric index. 

Test script:
---------------
<?php

preg_match_all(
    '/This message contains (?<items>[\d]+) items/',
    'This message contains 42 items',
    $matches
);
print_r($matches);

Expected result:
----------------
Array
(
    [0] => Array
        (
            [0] => This message contains 42 items
        )

    [items] => Array
        (
            [0] => 42
        )
)

Actual result:
--------------
Array
(
    [0] => Array
        (
            [0] => This message contains 42 items
        )

    [items] => Array
        (
            [0] => 42
        )

    [1] => Array
        (
            [0] => 42
        )
)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-05-17 06:06 UTC] danielklein at airpost dot net
I was just about to post the same thing! I would want a flag to pass in to preg_match() and preg_match_all(), something like PREG_PREFER_NAMED_CAPTURES.
 [2016-08-20 10:28 UTC] cmb@php.net
-Package: *Regular Expressions +Package: PCRE related
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 12:01:25 2019 UTC