Bug #73948 [Fbk->Opn]: Preg_match_all should return NULLs on trailing optional capture groups.
| From: | requinix@php.net | Date: | Mon, 16 Jan 2017 15:50:24 +0000 |
| Subject: | Bug #73948 [Fbk->Opn]: Preg_match_all should return NULLs on trailing optional capture groups. | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-206681@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=73948&edit=1
ID: 73948
Updated by: requinix@php.net
Reported by: tomasyorke at hotmail dot com
-Summary: Preg_match_all should return empty strings on
optional capture groups.
+Summary: Preg_match_all should return NULLs on trailing
optional capture groups.
-Status: Feedback
+Status: Open
Type: Bug
Package: PCRE related
Operating System: Windows 7
PHP Version: 7.0.14
Block user comment: N
Private report: N
New Comment:
Actually I can. Yay for Linux on Windows!
~/php/master/bin# ./php
<?php var_dump(preg_match('/(a)?(b)?(c)?/', 'b', $matches), $matches);
int(1)
array(3) {
[0]=>
string(1) "b"
[1]=>
NULL
[2]=>
string(1) "b"
}
[1] is clearly NULL but there is no [3] for the unmatched (c)? group.
Previous Comments:
------------------------------------------------------------------------
[2017-01-16 14:57:13] requinix@php.net
Hmm, yes, I misunderstood this report: 61780 is about turning empty strings from unmatched
subpatterns into NULLs while this is about *adding* trailing unmatched subpatterns.
I don't see anything in the 61780 changes that clearly indicate this is also fixed, but some
parts look like they might do it accidentally. Can anyone check? (It'd be nice if 3v4l could do
master too...)
------------------------------------------------------------------------
[2017-01-16 14:40:41] nikic@php.net
Not sure if it's quite a duplicate. IIRC we now use null instead of "" if unmatched,
but we still don't fill up null values at the end.
------------------------------------------------------------------------
[2017-01-16 14:38:08] requinix@php.net
Duplicate of bug #61780, fixed in master. (unmatched groups will be NULL)
------------------------------------------------------------------------
[2017-01-16 14:24:55] tomasyorke at hotmail dot com
Description:
------------
Using preg_match_all with the PREG_SET_ORDER flag and an optional capture group might return either
an empty string or a missing element.
This depends on whether there is a matched capture group after the non-matched capture group.
From an interface perspective, this means that I have to check for two representations to test
whether an optional capture group was matched.
The test script demonstrates such a case.
When considering a fix, the first priority should be that whatever the representation is (NULL, an
empty string or a missing element.), it should be consistent, no matter if there is a matched
capture group after or not.
If this cannot be fixed due to backwards compatibility Issues. Could we add a PREG_KEEP_NONMATCHES
or PREG_SET_ORDER_2 flag?
Test script:
---------------
<?php
preg_match_all("#(a)?(b)(c)?#","b",$matches,PREG_SET_ORDER);
var_dump($matches);
Expected result:
----------------
array(1) {
[0] => array(3) {
[0] => string(1) "b" [1] => string(0) "" [2] => string(1)
"b" [3] => string(0) ""
}
}
Actual result:
--------------
array(1) {
[0] => array(3) {
[0] => string(1) "b" [1] => string(0) "" [2] => string(1)
"b"
}
}
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=73948&edit=1