From 4f6088b10c5b3d04893cc165d04a9353361646be Mon Sep 17 00:00:00 2001 From: Patrick O'Meara Date: Tue, 2 Apr 2024 00:42:54 +1100 Subject: [PATCH] [11.x] Str trim methods (#50822) * Add trim method to Str to remove unicode space characters * Add ltrim and rtrim methods * Comments * styleCI patch * Assert that new lines are removed * Handle binary data * Use in middleware * Implement @dasundev's changes from #50832 --- .../Http/Middleware/TrimStrings.php | 3 +- src/Illuminate/Support/Str.php | 50 +++++++++++- src/Illuminate/Support/Stringable.php | 6 +- tests/Support/SupportStrTest.php | 77 +++++++++++++++++++ 4 files changed, 131 insertions(+), 5 deletions(-) diff --git a/src/Illuminate/Foundation/Http/Middleware/TrimStrings.php b/src/Illuminate/Foundation/Http/Middleware/TrimStrings.php index 0edc84376205..b82bdbfa8127 100644 --- a/src/Illuminate/Foundation/Http/Middleware/TrimStrings.php +++ b/src/Illuminate/Foundation/Http/Middleware/TrimStrings.php @@ -4,6 +4,7 @@ use Closure; use Illuminate\Support\Arr; +use Illuminate\Support\Str; class TrimStrings extends TransformsRequest { @@ -65,7 +66,7 @@ protected function transform($key, $value) return $value; } - return preg_replace('~^[\s\x{FEFF}\x{200B}\x{200E}]+|[\s\x{FEFF}\x{200B}\x{200E}]+$~u', '', $value) ?? trim($value); + return Str::trim($value); } /** diff --git a/src/Illuminate/Support/Str.php b/src/Illuminate/Support/Str.php index 39e303cae3b1..679e86bbb3f6 100644 --- a/src/Illuminate/Support/Str.php +++ b/src/Illuminate/Support/Str.php @@ -1418,6 +1418,54 @@ public static function snake($value, $delimiter = '_') return static::$snakeCache[$key][$delimiter] = $value; } + /** + * Remove all whitespace from both ends of a string. + * + * @param string $value + * @param string|null $charlist + * @return string + */ + public static function trim($value, $charlist = null) + { + if ($charlist === null) { + return preg_replace('~^[\s\x{FEFF}\x{200B}\x{200E}]+|[\s\x{FEFF}\x{200B}\x{200E}]+$~u', '', $value) ?? trim($value); + } + + return trim($value, $charlist); + } + + /** + * Remove all whitespace from the beginning of a string. + * + * @param string $value + * @param string|null $charlist + * @return string + */ + public static function ltrim($value, $charlist = null) + { + if ($charlist === null) { + return preg_replace('~^[\s\x{FEFF}\x{200B}\x{200E}]+~u', '', $value) ?? ltrim($value); + } + + return ltrim($value, $charlist); + } + + /** + * Remove all whitespace from the end of a string. + * + * @param string $value + * @param string|null $charlist + * @return string + */ + public static function rtrim($value, $charlist = null) + { + if ($charlist === null) { + return preg_replace('~[\s\x{FEFF}\x{200B}\x{200E}]+$~u', '', $value) ?? rtrim($value); + } + + return rtrim($value, $charlist); + } + /** * Remove all "extra" blank space from the given string. * @@ -1426,7 +1474,7 @@ public static function snake($value, $delimiter = '_') */ public static function squish($value) { - return preg_replace('~(\s|\x{3164}|\x{1160})+~u', ' ', preg_replace('~^[\s\x{FEFF}]+|[\s\x{FEFF}]+$~u', '', $value)); + return preg_replace('~(\s|\x{3164}|\x{1160})+~u', ' ', static::trim($value)); } /** diff --git a/src/Illuminate/Support/Stringable.php b/src/Illuminate/Support/Stringable.php index 8fe1f9e39a1d..defbb6c135a2 100644 --- a/src/Illuminate/Support/Stringable.php +++ b/src/Illuminate/Support/Stringable.php @@ -968,7 +968,7 @@ public function take(int $limit) */ public function trim($characters = null) { - return new static(trim(...array_merge([$this->value], func_get_args()))); + return new static(Str::trim(...array_merge([$this->value], func_get_args()))); } /** @@ -979,7 +979,7 @@ public function trim($characters = null) */ public function ltrim($characters = null) { - return new static(ltrim(...array_merge([$this->value], func_get_args()))); + return new static(Str::ltrim(...array_merge([$this->value], func_get_args()))); } /** @@ -990,7 +990,7 @@ public function ltrim($characters = null) */ public function rtrim($characters = null) { - return new static(rtrim(...array_merge([$this->value], func_get_args()))); + return new static(Str::rtrim(...array_merge([$this->value], func_get_args()))); } /** diff --git a/tests/Support/SupportStrTest.php b/tests/Support/SupportStrTest.php index 844c52b781bc..9670e9672ef3 100755 --- a/tests/Support/SupportStrTest.php +++ b/tests/Support/SupportStrTest.php @@ -766,6 +766,83 @@ public function testSnake() $this->assertSame('żółtałódka', Str::snake('ŻółtaŁódka')); } + public function testTrim() + { + $this->assertSame('foo bar', Str::trim(' foo bar ')); + $this->assertSame('foo bar', Str::trim('foo bar ')); + $this->assertSame('foo bar', Str::trim(' foo bar')); + $this->assertSame('foo bar', Str::trim('foo bar')); + $this->assertSame(' foo bar ', Str::trim(' foo bar ', '')); + $this->assertSame('foo bar', Str::trim(' foo bar ', ' ')); + $this->assertSame('foo bar', Str::trim('-foo bar_', '-_')); + + $this->assertSame('foo bar', Str::trim(' foo bar ')); + + $this->assertSame('123', Str::trim('  123   ')); + $this->assertSame('だ', Str::trim('だ')); + $this->assertSame('ム', Str::trim('ム')); + $this->assertSame('だ', Str::trim('  だ   ')); + $this->assertSame('ム', Str::trim('  ム   ')); + + $this->assertSame( + 'foo bar', + Str::trim(' + foo bar + ') + ); + $this->assertSame( + 'foo + bar', + Str::trim(' + foo + bar + ') + ); + + $this->assertSame("\xE9", Str::trim(" \xE9 ")); + } + + public function testLtrim() + { + $this->assertSame('foo bar ', Str::ltrim(' foo bar ')); + + $this->assertSame('123   ', Str::ltrim('  123   ')); + $this->assertSame('だ', Str::ltrim('だ')); + $this->assertSame('ム', Str::ltrim('ム')); + $this->assertSame('だ   ', Str::ltrim('  だ   ')); + $this->assertSame('ム   ', Str::ltrim('  ム   ')); + + $this->assertSame( + 'foo bar + ', + Str::ltrim(' + foo bar + ') + ); + $this->assertSame("\xE9 ", Str::ltrim(" \xE9 ")); + } + + public function testRtrim() + { + $this->assertSame(' foo bar', Str::rtrim(' foo bar ')); + + $this->assertSame('  123', Str::rtrim('  123   ')); + $this->assertSame('だ', Str::rtrim('だ')); + $this->assertSame('ム', Str::rtrim('ム')); + $this->assertSame('  だ', Str::rtrim('  だ   ')); + $this->assertSame('  ム', Str::rtrim('  ム   ')); + + $this->assertSame( + ' + foo bar', + Str::rtrim(' + foo bar + ') + ); + + $this->assertSame(" \xE9", Str::rtrim(" \xE9 ")); + } + public function testSquish() { $this->assertSame('laravel php framework', Str::squish(' laravel php framework '));