Skip to content

Commit

Permalink
[11.x] Str trim methods (#50822)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
patrickomeara authored Apr 1, 2024
1 parent 6f44f16 commit 4f6088b
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/Illuminate/Foundation/Http/Middleware/TrimStrings.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Closure;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;

class TrimStrings extends TransformsRequest
{
Expand Down Expand Up @@ -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);
}

/**
Expand Down
50 changes: 49 additions & 1 deletion src/Illuminate/Support/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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));
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/Illuminate/Support/Stringable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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())));
}

/**
Expand All @@ -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())));
}

/**
Expand All @@ -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())));
}

/**
Expand Down
77 changes: 77 additions & 0 deletions tests/Support/SupportStrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 '));
Expand Down

0 comments on commit 4f6088b

Please sign in to comment.