-
Notifications
You must be signed in to change notification settings - Fork 47
Add REGEXP_REPLACE support (including unit test) #120
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
base: develop
Are you sure you want to change the base?
Changes from all commits
421e08d
ddc87fa
c0d5dea
7ff5035
05d20b2
28d2b76
f521f5a
cfb123c
0ade800
380e541
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 |
---|---|---|
|
@@ -68,6 +68,7 @@ public function __construct( $pdo ) { | |
'isnull' => 'isnull', | ||
'if' => '_if', | ||
'regexp' => 'regexp', | ||
'regexp_replace' => 'regexp_replace', | ||
'field' => 'field', | ||
'log' => 'log', | ||
'least' => 'least', | ||
|
@@ -493,6 +494,54 @@ public function regexp( $pattern, $field ) { | |
return preg_match( $pattern, $field ); | ||
} | ||
|
||
/** | ||
* Method to emulate MySQL REGEXP_REPLACE() function. | ||
* | ||
* @param string|array $pattern Regular expression to search for (or array of strings). | ||
* @param string|array $replacement The string or an array with strings to replace. | ||
* @param string|array $field Haystack. | ||
* | ||
* @return Array if the field parameter is an array, or a string otherwise. | ||
*/ | ||
public function regexp_replace( $field, $pattern, $replacement ) { | ||
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. I just checked and in MySQL REGEXP_REPLACE() also takes three optional parameters:
Would you be up for adding a support for those? If not, that's fine, but let's at add them to the function signature and fail with an informative warning ( 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.
I will try to do that! |
||
/* | ||
* If the original query says REGEXP BINARY | ||
* the comparison is byte-by-byte and letter casing now | ||
* matters since lower- and upper-case letters have different | ||
* byte codes. | ||
* | ||
* The REGEXP function can't be easily made to accept two | ||
* parameters, so we'll have to use a hack to get around this. | ||
* | ||
* If the first character of the pattern is a null byte, we'll | ||
* remove it and make the comparison case-sensitive. This should | ||
* be reasonably safe since PHP does not allow null bytes in | ||
* regular expressions anyway. | ||
*/ | ||
Comment on lines
+507
to
+520
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. Does regexp_replace support 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. Isn't the binary-mode something global? |
||
|
||
/* Return null if one of the required parameter is null */ | ||
if ( is_null( $field ) || is_null( $pattern ) || is_null( $replacement ) ) { | ||
return null; | ||
} | ||
|
||
/* Return null if the pattern is empty - this changes MySQL/MariaDB behavior! */ | ||
if ( empty( $pattern ) ) { | ||
return null; | ||
} | ||
Comment on lines
+527
to
+530
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. Oh! Wha is the MySQL/MariaDB behavior? to just use an empty pattern? If so, let's do that. 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. It is throwing an error (because without pattern it can't work at all):
@bgrgicak was suggesting to return null instead to avoid breaking things, I assume. 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. Null seemed like a better response from an error. @Zodiac1978 let's update the comment to make it clear what this changes MySQL/MariaDB behavior means. |
||
|
||
if ( "\x00" === $pattern[0] ) { | ||
$pattern = substr( $pattern, 1 ); | ||
$flags = ''; | ||
} else { | ||
// Otherwise, the search is case-insensitive. | ||
$flags = 'i'; | ||
} | ||
$pattern = str_replace( '/', '\/', $pattern ); | ||
$pattern = '/' . $pattern . '/' . $flags; | ||
|
||
return preg_replace( $pattern, $replacement, $field ); | ||
} | ||
|
||
/** | ||
* Method to emulate MySQL FIELD() function. | ||
* | ||
|
Uh oh!
There was an error while loading. Please reload this page.