php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #71137 matches array should be indexed with pattern
Submitted: 2015-12-16 15:59 UTC Modified: 2016-03-20 18:27 UTC
From: g dot statkute at gmail dot com Assigned:
Status: Wont fix Package: PCRE related
PHP Version: Irrelevant OS: Windows
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.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: g dot statkute at gmail dot com
New email:
PHP Version: OS:

 

 [2015-12-16 15:59 UTC] g dot statkute at gmail dot com
Description:
------------
1) It would be much comfortable that array would be indexed with pattern as a string key.

    $subject = " 12,5 km";
// user can input kilometers, miles, degrees, radians
    $pattern = '/(km)|(kilometers)|(K)|(miles)|(m)|(degrees)|(deg)|(dg)|(d)|(radians)|(rad)|(rd)|(r)/';
    preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE);
    print_r('<pre>'); print_r($matches); print_r('</pre>');
    // returns array([0]=>array([0]=>'km' ,[1]=>'6'), [1]=>array([0]=>'km' ,[1]=>'6') )

It would be much comfortable that array would be indexed with pattern as a string key. In given case it could return instead:
array(['km']=>'km' ,[1]=>'6').

This would help to identify not only the search result, but also the property. In this case i would know instantly that user inputs kilometers.

2) It would be much more comfortable that all related information would also be presented as string keys, where 'pos' means position.
 Thus the first example would return:
array(['km']=>'km' ,['pos']=>'6').

And this example would return
    $pattern = '/(\d+)[,.;](\d+)/'; 
    $subject = " 12,5 km";
    preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE);
    print_r('<pre>'); print_r($matches); print_r('</pre>');
// prints: array([0]=>array([0]=>'12.5' ,[1]=>'1'), [1]=>array([0]=>12 ,[1]=>1), [2]=>array([0]=>5 ,[1]=>4) )
would return instead  array( 
array (['(\d+)[,.;](\d+)']=>'12.5' ,['pos']=>1 ),
array (['(\d+)']=>
            array( ['(\d+)']=>12, ['pos']=>1) ),
            array( ['(\d+)']=>12, ['pos']=>4) ),
  ),
)

Test script:
---------------
    $subject = " 12,5 km";
// user can input kilometers, miles, degrees, radians
    $pattern = '/(km)|(kilometers)|(K)|(miles)|(m)|(degrees)|(deg)|(dg)|(d)|(radians)|(rad)|(rd)|(r)/';
    preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE);
    print_r('<pre>'); print_r($matches); print_r('</pre>');
    // returns array([0]=>array([0]=>'km' ,[1]=>'6'), [1]=>array([0]=>'km' ,[1]=>'6') )


    $pattern = '/(\d+)[,.;](\d+)/'; 
    $subject = " 12,5 km";
    preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE);
    print_r('<pre>'); print_r($matches); print_r('</pre>');
// prints: array([0]=>array([0]=>'12.5' ,[1]=>'1'), [1]=>array([0]=>12 ,[1]=>1), [2]=>array([0]=>5 ,[1]=>4) )


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-03-20 18:27 UTC] nikic@php.net
-Status: Open +Status: Wont fix
 [2016-03-20 18:27 UTC] nikic@php.net
If you want to have named subpatterns, then you should use named subpatterns. PCRE supports these using the syntax (?<name>...), (?P<name>...) or (?'name'...). However, in your particular case it would seem prudent to simply use a single capturing group instead, i.e. write (km|kilometers|K|...) instead of (km)|(kilometers)|(K)|...

Renaming the name of the index for offset capture is not going to happen. We can't change it due to BC concerns, and introducing new flags for this kind of cosmetic change is not worthwhile.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 21:01:27 2024 UTC