Req #71137 [Opn->Wfx]: matches array should be indexed with pattern

From: Date: Sun, 20 Mar 2016 18:27:26 +0000
Subject: Req #71137 [Opn->Wfx]: matches array should be indexed with pattern
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-199975@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=71137&edit=1

 ID:                 71137
 Updated by:         nikic@php.net
 Reported by:        g dot statkute at gmail dot com
 Summary:            matches array should be indexed with pattern
-Status:             Open
+Status:             Wont fix
 Type:               Feature/Change Request
 Package:            PCRE related
 Operating System:   Windows
 PHP Version:        Irrelevant
 Block user comment: N
 Private report:     N

 New Comment:

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.


Previous Comments:
------------------------------------------------------------------------
[2015-12-16 15:59:43] 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) )



------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=71137&edit=1


Thread (2 messages)

« previous php.bugs (#199975) next »