forked from WordPress/sqlite-database-integration
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First draft of adding regexp_replace function to SQLite
Fixes WordPress#47
- Loading branch information
1 parent
8a4efbe
commit 421e08d
Showing
1 changed file
with
38 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
|
@@ -492,6 +493,43 @@ 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( $pattern, $replacement, $field ) { | ||
/* | ||
* 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. | ||
*/ | ||
if ( "\x00" === $pattern[0] ) { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
Zodiac1978
Author
Owner
|
||
$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. | ||
* | ||
|
Let's check if
$pattern
is a non-empty string. That may also be applicable to the other regexp function.