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
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: gooh@php.net
New email:
PHP Version: OS:

 

 [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

Pull Requests

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-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 18:01:29 2024 UTC