Bug->Doc #66664 [Opn]: preg_match_all + PREG_PATTERN_ORDER + PCRE_INFO_JCHANGED, output wrong result
| From: | cmb@php.net | Date: | Mon, 13 Apr 2015 14:22:40 +0000 |
| Subject: | Bug->Doc #66664 [Opn]: preg_match_all + PREG_PATTERN_ORDER + PCRE_INFO_JCHANGED, output wrong result | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-12141@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=66664&edit=1
ID: 66664
Updated by: cmb@php.net
Reported by: coolwust at gmail dot com
Summary: preg_match_all + PREG_PATTERN_ORDER +
PCRE_INFO_JCHANGED, output wrong result
Status: Open
-Type: Bug
+Type: Documentation Problem
Package: PCRE related
Operating System: 3.12-1-amd64 Debian 3.12.
PHP Version: 5.5.9
Block user comment: N
Private report: N
New Comment:
> Maybe the latest named capturing overwrite the previous result.
Yes, that is exactly what's happening. Consider the following
simplified test script:
preg_match_all(
'/(?J)(?<match>foo)|(?<match>bar)/',
'foo bar',
$matches,
PREG_PATTERN_ORDER
);
var_dump($matches);
which outputs:
array(4) {
[0] =>
array(2) {
[0] =>
string(3) "foo"
[1] =>
string(3) "bar"
}
'match' =>
array(2) {
[0] =>
string(0) ""
[1] =>
string(3) "bar"
}
[1] =>
array(2) {
[0] =>
string(3) "foo"
[1] =>
string(0) ""
}
[2] =>
array(2) {
[0] =>
string(0) ""
[1] =>
string(3) "bar"
}
}
And actually, that behavior is pretty much to be expected when
using duplicate named subpatterns in combination with
preg_match_all() and PREG_PATTERN_ORDER.
It seems to me that this is not a bug, but rather something that
should be better documented.
Previous Comments:
------------------------------------------------------------------------
[2014-02-07 19:03:41] coolwust at gmail dot com
Description:
------------
---
From manual page: http://www.php.net/function.preg-match-all
---
The code is from pcre manual, chapter 'NAMED SUBPATTERNS', line 6093:
http://www.pcre.org/pcre.txt
PREG_SET_ORDER:
Output the same result as the manual expected.
PREG_PATTERN_ORDER
Output the wrong result. Please check example for details.
Maybe the latest named capturing overwrite the previous result.
Test script:
---------------
$pattern = <<<'PATTERN'
/(?xJ)
(?<DN>Mon|Fri|Sun)(?:day)?|
(?<DN>Tue)(?:sday)?|
(?<DN>Wed)(?:nesday)?|
(?<DN>Thu)(?:rsday)?|
(?<DN>Sat)(?:urday)?
/
PATTERN;
$subject = "Monday\nThursday";
$result1 = preg_match_all($pattern, $subject, $matches1, PREG_PATTERN_ORDER);
$result2 = preg_match_all($pattern, $subject, $matches2, PREG_SET_ORDER);
echo 'subject:';
var_dump($subject);
echo 'pattern:';
var_dump($pattern);
echo 'flag PREG_PATTERN_ORDER:';
var_dump($matches1);
echo 'flag PREG_SET_ORDER:';
var_dump($matches2);
Expected result:
----------------
subject:
string 'Monday
Thursday' (length=15)
pattern:
string '/(?xJ)
(?<DN>Mon|Fri|Sun)(?:day)?|
(?<DN>Tue)(?:sday)?|
(?<DN>Wed)(?:nesday)?|
(?<DN>Thu)(?:rsday)?|
(?<DN>Sat)(?:urday)?
/' (length=123)
flag PREG_PATTERN_ORDER:
array (size=7)
0 =>
array (size=2)
0 => string 'Monday' (length=6)
1 => string 'Thursday' (length=8)
'DN' =>
array (size=2)
0 => string 'Mon' (length=0) //Expected
1 => string 'Thu' (length=0) //Expected
1 =>
array (size=2)
0 => string 'Mon' (length=3)
1 => string '' (length=0)
2 =>
array (size=2)
0 => string '' (length=0)
1 => string '' (length=0)
3 =>
array (size=2)
0 => string '' (length=0)
1 => string '' (length=0)
4 =>
array (size=2)
0 => string '' (length=0)
1 => string 'Thu' (length=3)
5 =>
array (size=2)
0 => string '' (length=0)
1 => string '' (length=0)
flag PREG_SET_ORDER:
array (size=2)
0 =>
array (size=3)
0 => string 'Monday' (length=6)
'DN' => string 'Mon' (length=3)
1 => string 'Mon' (length=3)
1 =>
array (size=6)
0 => string 'Thursday' (length=8)
'DN' => string 'Thu' (length=3)
1 => string '' (length=0)
2 => string '' (length=0)
3 => string '' (length=0)
4 => string 'Thu' (length=3)
Actual result:
--------------
subject:
string 'Monday
Thursday' (length=15)
pattern:
string '/(?xJ)
(?<DN>Mon|Fri|Sun)(?:day)?|
(?<DN>Tue)(?:sday)?|
(?<DN>Wed)(?:nesday)?|
(?<DN>Thu)(?:rsday)?|
(?<DN>Sat)(?:urday)?
/' (length=123)
flag PREG_PATTERN_ORDER:
array (size=7)
0 =>
array (size=2)
0 => string 'Monday' (length=6)
1 => string 'Thursday' (length=8)
'DN' =>
array (size=2)
0 => string '' (length=0) //Wrong
1 => string '' (length=0) //Wrong
1 =>
array (size=2)
0 => string 'Mon' (length=3)
1 => string '' (length=0)
2 =>
array (size=2)
0 => string '' (length=0)
1 => string '' (length=0)
3 =>
array (size=2)
0 => string '' (length=0)
1 => string '' (length=0)
4 =>
array (size=2)
0 => string '' (length=0)
1 => string 'Thu' (length=3)
5 =>
array (size=2)
0 => string '' (length=0)
1 => string '' (length=0)
flag PREG_SET_ORDER:
array (size=2)
0 =>
array (size=3)
0 => string 'Monday' (length=6)
'DN' => string 'Mon' (length=3)
1 => string 'Mon' (length=3)
1 =>
array (size=6)
0 => string 'Thursday' (length=8)
'DN' => string 'Thu' (length=3)
1 => string '' (length=0)
2 => string '' (length=0)
3 => string '' (length=0)
4 => string 'Thu' (length=3)
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=66664&edit=1