You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
preg_grep should return false when preg_match does #11936
No? The purpose of preg_grep is to find all strings in an array that match a pattern, so the function should always be returning an array, and if none of the strings match the pattern then the array should be empty. Making it return different data types when successful makes userland code more awkward.
Yes, empty/incomplete array result is simply wrong and inconstent, as all other preg_* functions return false when preg_last_error() is non-zero. So IMHO it is a bug and should be fixed.
I missed that this was an example of a /u regex on a non-UTF-8 string. Let me say that up front.
If there is an error affecting preg_grep's ability to do any matching at all, like a bad regex, then it should return false. Naturally.
If there is an error affecting its ability to do matching on a particular input string then I think it should make a best effort to continue doing what it can - ie. continue and attempt to match the remaining strings - however (a) I can see an argument for it "giving up" and returning false, but moreover (b) this would require having a way to get multiple "last errors", which does not exist. So I'll concede that returning false is all it can do... in lieu of a "preg_last_errors" function. Meaning I think the best solution to this is to implement such a function, that returns an array of [code=>N, msg=>string] pairs, probably including the input strings as well, and tie all the PCRE functions into it. Which is more work. Either way, though, there is a bug here.
If there are no errors and it's simply a matter of none of the input strings matching then preg_grep should return an empty array. This is what I thought the issue was about.
(Actually, what I really think is best is that preg_grep returns an array of match/no-match/is-error Enums, but that ship has sailed.)
Activity
damianwadley commentedon Aug 11, 2023
No? The purpose of preg_grep is to find all strings in an array that match a pattern, so the function should always be returning an array, and if none of the strings match the pattern then the array should be empty. Making it return different data types when successful makes userland code more awkward.
mvorisek commentedon Aug 12, 2023
Matching pattern with
umodifier againts non-UTF-8 input must emit an error. Until preg_* functions throw on error,falsereturn type is expected.KapitanOczywisty commentedon Aug 12, 2023
After error
preg_grepalso stops searching in remaining elements: https://3v4l.org/o7uDfmvorisek commentedon Aug 12, 2023
Yes, empty/incomplete array result is simply wrong and inconstent, as all other preg_* functions return
falsewhenpreg_last_error()is non-zero. So IMHO it is a bug and should be fixed.damianwadley commentedon Aug 12, 2023
I missed that this was an example of a /u regex on a non-UTF-8 string. Let me say that up front.
Meaning I think the best solution to this is to implement such a function, that returns an array of
[code=>N, msg=>string]pairs, probably including the input strings as well, and tie all the PCRE functions into it. Which is more work.Either way, though, there is a bug here.
(Actually, what I really think is best is that preg_grep returns an array of match/no-match/is-error Enums, but that ship has sailed.)
mvorisek commentedon Apr 2, 2024
Can the
Status: Needs Triagelabel be removed?ext/pcre: Fix preg_grep() returning partial array instead of false on…