Skip to content

Commit d7e262e

Browse files
committed
bug #507 mbstring: Fix mb_rtrim() for UTF-8 text (nsfisis)
This PR was merged into the 1.x branch. Discussion ---------- mbstring: Fix mb_rtrim() for UTF-8 text # Component Polyfill-mbstring # Problem `mb_rtrim()` returns an empty string for UTF-8 text. # How to Reproduce ```php echo \Symfony\Polyfill\Mbstring\Mbstring::mb_rtrim(' あいうえお '); // => '' (empty string) ``` Expected result: `' あいうえお'` # Cause The regular expression used in `mb_rtrim()` implementation does not have `u` flag. # Changes * Add `u` flag to the regular expression used in `mb_rtrim()` implementation as `mb_trim()` and `mb_ltrim()` do. * Add tests. Commits ------- 65d1260 mbstring: Fix mb_rtrim() for UTF-8 text
2 parents 2ef3477 + 65d1260 commit d7e262e

File tree

2 files changed

+3
-1
lines changed

2 files changed

+3
-1
lines changed

src/Mbstring/Mbstring.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -983,7 +983,7 @@ public static function mb_ltrim(string $string, ?string $characters = null, ?str
983983

984984
public static function mb_rtrim(string $string, ?string $characters = null, ?string $encoding = null): string
985985
{
986-
return self::mb_internal_trim('{[%s]+$}D', $string, $characters, $encoding, __FUNCTION__);
986+
return self::mb_internal_trim('{[%s]+$}Du', $string, $characters, $encoding, __FUNCTION__);
987987
}
988988

989989
private static function mb_internal_trim(string $regex, string $string, ?string $characters, ?string $encoding, string $function): string

tests/Mbstring/MbstringTest.php

+2
Original file line numberDiff line numberDiff line change
@@ -886,6 +886,7 @@ public static function mbLTrimProvider(): iterable
886886
yield [' test ', ' test ', ''];
887887

888888
yield ['いああああ', 'あああああああああああああああああああああああああああああああああいああああ', ''];
889+
yield ['あいうえお ', ' あいうえお '];
889890

890891
yield ['漢字', "\u{FFFE}漢字", "\u{FFFE}\u{FEFF}"];
891892
yield [' abcd ', ' abcd ', ''];
@@ -902,6 +903,7 @@ public static function mbRTrimProvider(): iterable
902903
yield [' a', str_repeat(' ', 129).'a'];
903904

904905
yield ['あああああああああああああああああああああああああああああああああい', 'あああああああああああああああああああああああああああああああああいああああ', ''];
906+
yield [' あいうえお', ' あいうえお '];
905907

906908
yield [' abcd ', ' abcd ', ''];
907909

0 commit comments

Comments
 (0)