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
Have you experienced this issue?
Rate the importance of this bug to you:

 [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-2020 The PHP Group
All rights reserved.
Last updated: Sat Oct 31 14:01:24 2020 UTC