From: g dot statkute at gmail dot com
Operating system: Windows
PHP version: Irrelevant
Package: PCRE related
Bug Type: Feature/Change Request
Bug description:matches array should be indexed with pattern
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 bug report at https://bugs.php.net/bug.php?id=71137&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=71137&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=71137&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=71137&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=71137&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=71137&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=71137&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=71137&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=71137&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=71137&r=support
Expected behavior: https://bugs.php.net/fix.php?id=71137&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=71137&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=71137&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=71137&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=71137&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=71137&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=71137&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=71137&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=71137&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=71137&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=71137&r=mysqlcfg