@@ -107,11 +107,8 @@ public static function register_for( $pdo ): self {
107107 'regexp_instr ' => array ( 2 , 3 , 4 , 5 , 6 ),
108108 );
109109
110- /** @var string|null Last validated regex pattern. */
111- private $ regexp_cached_pattern = null ;
112-
113- /** @var string|null Match type used by the cached regex. */
114- private $ regexp_cached_match_type = null ;
110+ /** @var array|null Pattern, match type, and byte mode of the last compiled regex. */
111+ private $ regexp_cached_key = null ;
115112
116113 /** @var string|null Last compiled PCRE pattern. */
117114 private $ regexp_cached_compiled = null ;
@@ -597,12 +594,18 @@ public function isnull( $field ) {
597594 /**
598595 * Method to emulate MySQL REGEXP() function.
599596 *
600- * @param string $pattern Regular expression to match.
601- * @param string $field Haystack.
597+ * @param string|null $pattern Regular expression to match.
598+ * @param string|null $field Haystack.
602599 *
603- * @return integer 1 if matched, 0 if not matched.
600+ * @throws Exception If the pattern is not a valid regular expression.
601+ * @return int|null 1 if matched, 0 if not matched, or NULL for NULL input.
604602 */
605603 public function regexp ( $ pattern , $ field ) {
604+ if ( null === $ pattern || null === $ field ) {
605+ return null ;
606+ }
607+ $ pattern = (string ) $ pattern ;
608+
606609 /*
607610 * If the original query says REGEXP BINARY
608611 * the comparison is byte-by-byte and letter casing now
@@ -617,17 +620,22 @@ public function regexp( $pattern, $field ) {
617620 * be reasonably safe since PHP does not allow null bytes in
618621 * regular expressions anyway.
619622 */
620- if ( "\x00" === $ pattern [0 ] ) {
623+ $ is_binary = '' !== $ pattern && "\x00" === $ pattern [0 ];
624+ if ( $ is_binary ) {
621625 $ pattern = substr ( $ pattern , 1 );
622- $ flags = '' ;
623- } else {
624- // Otherwise, the search is case-insensitive.
625- $ flags = 'i ' ;
626626 }
627- $ pattern = str_replace ( '/ ' , '\/ ' , $ pattern );
628- $ pattern = '/ ' . $ pattern . '/ ' . $ flags ;
629627
630- return preg_match ( $ pattern , $ field );
628+ /*
629+ * Data that is not valid UTF-8 can only be a binary string in MySQL,
630+ * and MySQL matches binary strings byte by byte and case-sensitively.
631+ */
632+ $ byte_mode = $ is_binary || ! $ this ->regexp_is_utf8 ( $ pattern ) || ! $ this ->regexp_is_utf8 ( $ field );
633+ $ compiled = $ this ->regexp_compile ( $ pattern , $ byte_mode ? 'c ' : '' , $ byte_mode );
634+ $ result = preg_match ( $ compiled , $ field );
635+ if ( false === $ result ) {
636+ $ this ->regexp_fail ( $ pattern );
637+ }
638+ return $ result ;
631639 }
632640
633641 /**
@@ -1206,9 +1214,9 @@ public function _helper_like_to_glob_pattern( $pattern ) {
12061214 /**
12071215 * Compile a MySQL-style regex into a PCRE pattern string.
12081216 *
1209- * Translates MySQL match_type flags (c/i/m/n/u) to PCRE modifiers and always
1210- * appends the u (UTF-8) modifier. Case-insensitive is the default, matching
1211- * the existing REGEXP operator .
1217+ * Translates MySQL match_type flags (c/i/m/n/u) to PCRE modifiers. The u
1218+ * (UTF-8) modifier is applied unless byte-oriented matching is requested.
1219+ * Case-insensitive is the default .
12121220 *
12131221 * MySQL's native engine is ICU; we use PHP's PCRE. The two diverge in a
12141222 * few corners:
@@ -1225,29 +1233,27 @@ public function _helper_like_to_glob_pattern( $pattern ) {
12251233 * `utf8mb4_0900_ai_ci` collation; callers that rely on a `_bin` or
12261234 * `_cs` collation must pass an explicit `c` match_type because this
12271235 * helper has no access to the session collation.
1228- * - The `u` (UTF-8) PCRE modifier is always applied. Binary data with
1229- * invalid UTF-8 bytes that matches fine under the legacy `REGEXP`
1230- * operator raises "Invalid UTF-8 data in regular expression input."
1231- * when routed through REGEXP_LIKE / _REPLACE / _SUBSTR / _INSTR .
1236+ * - The `u` (UTF-8) PCRE modifier is applied to REGEXP_* functions, so
1237+ * they reject data that is not valid UTF-8 from the start position on.
1238+ * The REGEXP operators match such data, and REGEXP BINARY, byte by byte,
1239+ * like MySQL binary strings .
12321240 *
1233- * @param string $pattern The MySQL regex pattern.
1234- * @param string $match_type MySQL match_type flag string.
1241+ * @param string $pattern The MySQL regex pattern.
1242+ * @param string $match_type MySQL match_type flag string.
1243+ * @param bool $binary_mode Whether to match bytes instead of UTF-8 characters.
12351244 *
12361245 * @throws Exception If the pattern is empty or the match_type string
12371246 * contains an unrecognized flag.
12381247 * @return string PCRE-ready pattern with delimiter and modifiers.
12391248 */
1240- private function regexp_compile ( $ pattern , $ match_type ) {
1249+ private function regexp_compile ( $ pattern , $ match_type, $ binary_mode = false ) {
12411250 $ match_type = (string ) $ match_type ;
12421251 $ pattern = (string ) $ pattern ;
12431252 if ( '' === $ pattern ) {
12441253 throw new Exception ( 'Illegal argument to a regular expression. ' );
12451254 }
1246- if (
1247- null !== $ pattern
1248- && $ pattern === $ this ->regexp_cached_pattern
1249- && $ match_type === $ this ->regexp_cached_match_type
1250- ) {
1255+ $ cache_key = array ( $ pattern , $ match_type , $ binary_mode );
1256+ if ( $ cache_key === $ this ->regexp_cached_key ) {
12511257 return $ this ->regexp_cached_compiled ;
12521258 }
12531259
@@ -1273,7 +1279,7 @@ private function regexp_compile( $pattern, $match_type ) {
12731279 }
12741280 }
12751281
1276- $ modifiers = 'u ' ;
1282+ $ modifiers = $ binary_mode ? '' : 'u ' ;
12771283 if ( ! $ case_sensitive ) {
12781284 $ modifiers .= 'i ' ;
12791285 }
@@ -1300,9 +1306,8 @@ private function regexp_compile( $pattern, $match_type ) {
13001306 if ( false === $ valid ) {
13011307 $ this ->regexp_fail ( $ pattern );
13021308 }
1303- $ this ->regexp_cached_pattern = $ pattern ;
1304- $ this ->regexp_cached_match_type = $ match_type ;
1305- $ this ->regexp_cached_compiled = $ compiled ;
1309+ $ this ->regexp_cached_key = $ cache_key ;
1310+ $ this ->regexp_cached_compiled = $ compiled ;
13061311 return $ compiled ;
13071312 }
13081313
@@ -1344,6 +1349,17 @@ private function regexp_escape_delimiter( $pattern ) {
13441349 return $ escaped ;
13451350 }
13461351
1352+ /**
1353+ * Check whether a string is valid UTF-8.
1354+ *
1355+ * @param string $value The string.
1356+ *
1357+ * @return bool Whether the string is valid UTF-8.
1358+ */
1359+ private function regexp_is_utf8 ( $ value ) {
1360+ return 1 === preg_match ( '//u ' , $ value );
1361+ }
1362+
13471363 /**
13481364 * Convert a 1-based character position into a byte offset into the UTF-8 string.
13491365 *
0 commit comments