Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -934,15 +934,20 @@ public function version() {
* Method to emulate MySQL REVERSE() function.
*
* Takes a string and returns the reverse of it.
* Compatible with MySQL behaviour for utf8mb4 strings.
*
* @param string|null $str The string to reverse.
*
* @return string|null reversed string, or NULL.
*/
* @param string|null $str The string to reverse.
*
* @return string|null reversed string, or NULL.
*/
public function reverse( $str ) {
if ( null === $str ) {
return null;
}
if ( preg_match( '/[^\x00-\x7F]/', $str ) ) {
preg_match_all( '/./u', $str, $matches );
return implode( '', array_reverse( $matches[0] ) );
}
return strrev( $str );
}

Expand Down
8 changes: 8 additions & 0 deletions packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -12585,6 +12585,14 @@ public function testReverseFunction(): void {
$result = $this->assertQuery( "SELECT REVERSE('aoeuidhtns') AS reversed" );
$this->assertSame( 'snthdiueoa', $result[0]->reversed );

// Unicode reverse.
$result = $this->assertQuery( "SELECT REVERSE('Héllö Wörld!') AS reversed" );
$this->assertSame( '!dlröW ölléH', $result[0]->reversed );

// Emoji reverse.
$result = $this->assertQuery( "SELECT REVERSE('Text with 😁😂🤣 emoji 🚀') AS reversed" );
$this->assertSame( '🚀 ijome 🤣😂😁 htiw txeT', $result[0]->reversed );

// Empty string.
$result = $this->assertQuery( "SELECT REVERSE('') AS reversed" );
$this->assertSame( '', $result[0]->reversed );
Expand Down
Loading