Open mvorisek opened 1 year ago
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.
Matching pattern with u
modifier againts non-UTF-8 input must emit an error. Until preg_* functions throw on error, false
return type is expected.
After error preg_grep
also stops searching in remaining elements: https://3v4l.org/o7uDf
<?php
var_dump(preg_grep('/.*/u', ['foo', 'bar']));
echo preg_last_error_msg(), "\n\n";
var_dump(preg_grep('/.*/u', ['foo', hex2bin('ff'), 'bar']));
echo preg_last_error_msg(), "\n\n";
array(2) {
[0]=>
string(3) "foo"
[1]=>
string(3) "bar"
}
No error
array(1) {
[0]=>
string(3) "foo"
}
Malformed UTF-8 characters, possibly incorrectly encoded
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.
[code=>N, msg=>string]
pairs, probably including the input strings as well, and tie all the PCRE functions into it. Which is more work.(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.)
Can the Status: Needs Triage
label be removed?
Description
The following code:
https://3v4l.org/kihD6
Resulted in this output:
But I expected this output instead:
PHP Version
any
Operating System
any