@@ -123,6 +123,9 @@ public static function register_for( $pdo ): self {
123123 /** @var string|null Match type used by the cached regex. */
124124 private $ regexp_cached_match_type = null ;
125125
126+ /** @var bool|null Whether the cached regex uses byte-oriented matching. */
127+ private $ regexp_cached_binary = null ;
128+
126129 /** @var string|null Last compiled PCRE pattern. */
127130 private $ regexp_cached_compiled = null ;
128131
@@ -622,12 +625,20 @@ public function _if( $expression, $truthy, $falsy ) {
622625 /**
623626 * Method to emulate MySQL REGEXP() function.
624627 *
625- * @param string $pattern Regular expression to match.
626- * @param string $field Haystack.
628+ * @param string|null $pattern Regular expression to match.
629+ * @param string|null $field Haystack.
627630 *
628- * @return integer 1 if matched, 0 if not matched.
631+ * @throws Exception If the pattern is not a valid regular expression.
632+ * @return int|null 1 if matched, 0 if not matched, or NULL for NULL input.
629633 */
630634 public function regexp ( $ pattern , $ field ) {
635+ if ( null === $ pattern || null === $ field ) {
636+ return null ;
637+ }
638+
639+ $ pattern = $ this ->regexp_string_arg ( $ pattern );
640+ $ field = $ this ->regexp_string_arg ( $ field );
641+
631642 /*
632643 * If the original query says REGEXP BINARY
633644 * the comparison is byte-by-byte and letter casing now
@@ -642,17 +653,21 @@ public function regexp( $pattern, $field ) {
642653 * be reasonably safe since PHP does not allow null bytes in
643654 * regular expressions anyway.
644655 */
645- if ( "\x00" === $ pattern [0 ] ) {
656+ $ is_binary = '' !== $ pattern && "\x00" === $ pattern [0 ];
657+ if ( $ is_binary ) {
646658 $ pattern = substr ( $ pattern , 1 );
647- $ flags = '' ;
648- } else {
649- // Otherwise, the search is case-insensitive.
650- $ flags = 'i ' ;
651659 }
652- $ pattern = str_replace ( '/ ' , '\/ ' , $ pattern );
653- $ pattern = '/ ' . $ pattern . '/ ' . $ flags ;
654660
655- return preg_match ( $ pattern , $ field );
661+ $ compiled = $ this ->regexp_compile ( $ pattern , $ is_binary ? 'c ' : '' , $ is_binary );
662+ $ result = $ this ->regexp_run (
663+ function () use ( $ compiled , $ field ) {
664+ return preg_match ( $ compiled , $ field );
665+ }
666+ );
667+ if ( false === $ result ) {
668+ $ this ->regexp_fail ( $ pattern );
669+ }
670+ return $ result ;
656671 }
657672
658673 /**
@@ -1271,9 +1286,9 @@ public function _helper_like_to_glob_pattern( $pattern ) {
12711286 /**
12721287 * Compile a MySQL-style regex into a PCRE pattern string.
12731288 *
1274- * Translates MySQL match_type flags (c/i/m/n/u) to PCRE modifiers and always
1275- * appends the u (UTF-8) modifier. Case-insensitive is the default, matching
1276- * the existing REGEXP operator .
1289+ * Translates MySQL match_type flags (c/i/m/n/u) to PCRE modifiers. The u
1290+ * (UTF-8) modifier is applied unless byte-oriented matching is requested.
1291+ * Case-insensitive is the default .
12771292 *
12781293 * MySQL's native engine is ICU; we use PHP's PCRE. The two diverge in a
12791294 * few corners:
@@ -1290,20 +1305,19 @@ public function _helper_like_to_glob_pattern( $pattern ) {
12901305 * `utf8mb4_0900_ai_ci` collation; callers that rely on a `_bin` or
12911306 * `_cs` collation must pass an explicit `c` match_type because this
12921307 * helper has no access to the session collation.
1293- * - The `u` (UTF-8) PCRE modifier is always applied. Binary data with
1294- * invalid UTF-8 bytes that matches fine under the legacy `REGEXP`
1295- * operator raises "Invalid UTF-8 data in regular expression input."
1296- * when routed through REGEXP_LIKE / _REPLACE / _SUBSTR / _INSTR.
1308+ * - The `u` (UTF-8) PCRE modifier is applied to REGEXP_* functions and
1309+ * nonbinary REGEXP operators. REGEXP BINARY remains byte-oriented.
12971310 *
1298- * @param string|null $pattern The MySQL regex pattern.
1299- * @param string $match_type MySQL match_type flag string.
1311+ * @param string|null $pattern The MySQL regex pattern.
1312+ * @param string $match_type MySQL match_type flag string.
1313+ * @param bool $binary_mode Whether to match bytes instead of UTF-8 characters.
13001314 *
13011315 * @throws Exception If the pattern is empty or the match_type string
13021316 * contains an unrecognized flag.
13031317 * @return string|null PCRE-ready pattern with delimiter and modifiers, or
13041318 * NULL when the pattern is NULL.
13051319 */
1306- private function regexp_compile ( $ pattern , $ match_type ) {
1320+ private function regexp_compile ( $ pattern , $ match_type, $ binary_mode = false ) {
13071321 $ match_type = $ this ->regexp_string_arg ( $ match_type );
13081322 if ( null !== $ pattern ) {
13091323 $ pattern = $ this ->regexp_string_arg ( $ pattern );
@@ -1315,6 +1329,7 @@ private function regexp_compile( $pattern, $match_type ) {
13151329 null !== $ pattern
13161330 && $ pattern === $ this ->regexp_cached_pattern
13171331 && $ match_type === $ this ->regexp_cached_match_type
1332+ && $ binary_mode === $ this ->regexp_cached_binary
13181333 ) {
13191334 return $ this ->regexp_cached_compiled ;
13201335 }
@@ -1341,7 +1356,7 @@ private function regexp_compile( $pattern, $match_type ) {
13411356 }
13421357 }
13431358
1344- $ modifiers = 'u ' ;
1359+ $ modifiers = $ binary_mode ? '' : 'u ' ;
13451360 if ( ! $ case_sensitive ) {
13461361 $ modifiers .= 'i ' ;
13471362 }
@@ -1367,6 +1382,7 @@ function () use ( $compiled ) {
13671382 }
13681383 $ this ->regexp_cached_pattern = $ pattern ;
13691384 $ this ->regexp_cached_match_type = $ match_type ;
1385+ $ this ->regexp_cached_binary = $ binary_mode ;
13701386 $ this ->regexp_cached_compiled = $ compiled ;
13711387 return $ compiled ;
13721388 }
0 commit comments