-
Notifications
You must be signed in to change notification settings - Fork 7.9k
ext/pcre - enable /n modifier #7583
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
--TEST-- | ||
testing /n modifier in preg_match() | ||
nikic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
--FILE-- | ||
<?php | ||
|
||
preg_match('/.(.)./n', 'abc', $m); | ||
var_dump($m); | ||
|
||
preg_match('/.(?P<test>.)./n', 'abc', $m); | ||
var_dump($m); | ||
|
||
?> | ||
--EXPECT-- | ||
array(1) { | ||
[0]=> | ||
string(3) "abc" | ||
} | ||
array(3) { | ||
[0]=> | ||
string(3) "abc" | ||
["test"]=> | ||
string(1) "b" | ||
[1]=> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How is producing a numbered capture "passing" when that's what the
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi Roy, this is same behavior according to Perl.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fair enough, but that's a pity since Perl provides a separate hash for named groups, but PHP gets the numbered and named groups lumped in the same array. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Not a problem there. PCRE behaves in this way, PHP is not creating this numbered entry. See PCRE2' documentation:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I understood that PCRE returns matches with both numbered and named references, but surely it has always been PHP's choice to put both of those reference sets into the one array and not provide a way to conditionally omit/separate the numbered set, has it not? This is unlike Perl which puts the named set into its own hash: echo abc | perl -ne '/(.)(?<x>.)(.)/n; $s = keys %+; print "$s\n"' There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The PCRE docs seems pretty clear cut on what the intention for this flag is ("and they acquire numbers in the usual way" and "though the reference can be by name or by number"). I don't see reason to deviate from that. Maybe there is room here for another modifier or option to only return named captures, but this modifier clearly isn't it. |
||
string(1) "b" | ||
} |
Uh oh!
There was an error while loading. Please reload this page.