Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.x] Str trim methods #50822

Merged
merged 8 commits into from
Apr 1, 2024
Merged
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
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