Skip to content
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
12 changes: 9 additions & 3 deletions src/Illuminate/Support/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -1497,7 +1497,9 @@ public static function snake($value, $delimiter = '_')
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);
$trimDefaultCharacters = " \n\r\t\v\0";

return preg_replace('~^[\s\x{FEFF}\x{200B}\x{200E}'.$trimDefaultCharacters.']+|[\s\x{FEFF}\x{200B}\x{200E}'.$trimDefaultCharacters.']+$~u', '', $value) ?? trim($value);
}

return trim($value, $charlist);
Expand All @@ -1513,7 +1515,9 @@ public static function trim($value, $charlist = null)
public static function ltrim($value, $charlist = null)
{
if ($charlist === null) {
return preg_replace('~^[\s\x{FEFF}\x{200B}\x{200E}]+~u', '', $value) ?? ltrim($value);
$ltrimDefaultCharacters = " \n\r\t\v\0";

return preg_replace('~^[\s\x{FEFF}\x{200B}\x{200E}'.$ltrimDefaultCharacters.']+~u', '', $value) ?? ltrim($value);
}

return ltrim($value, $charlist);
Expand All @@ -1529,7 +1533,9 @@ public static function ltrim($value, $charlist = null)
public static function rtrim($value, $charlist = null)
{
if ($charlist === null) {
return preg_replace('~[\s\x{FEFF}\x{200B}\x{200E}]+$~u', '', $value) ?? rtrim($value);
$rtrimDefaultCharacters = " \n\r\t\v\0";

return preg_replace('~[\s\x{FEFF}\x{200B}\x{200E}'.$rtrimDefaultCharacters.']+$~u', '', $value) ?? rtrim($value);
}

return rtrim($value, $charlist);
Expand Down
30 changes: 30 additions & 0 deletions tests/Support/SupportStrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -861,6 +861,16 @@ public function testTrim()
);

$this->assertSame("\xE9", Str::trim(" \xE9 "));

$trimDefaultChars = [' ', "\n", "\r", "\t", "\v", "\0"];

foreach ($trimDefaultChars as $char) {
$this->assertSame('', Str::trim(" {$char} "));
$this->assertSame(trim(" {$char} "), Str::trim(" {$char} "));

$this->assertSame('foo bar', Str::trim("{$char} foo bar {$char}"));
$this->assertSame(trim("{$char} foo bar {$char}"), Str::trim("{$char} foo bar {$char}"));
}
}

public function testLtrim()
Expand All @@ -881,6 +891,16 @@ public function testLtrim()
')
);
$this->assertSame("\xE9 ", Str::ltrim(" \xE9 "));

$ltrimDefaultChars = [' ', "\n", "\r", "\t", "\v", "\0"];

foreach ($ltrimDefaultChars as $char) {
$this->assertSame('', Str::ltrim(" {$char} "));
$this->assertSame(ltrim(" {$char} "), Str::ltrim(" {$char} "));

$this->assertSame("foo bar {$char}", Str::ltrim("{$char} foo bar {$char}"));
$this->assertSame(ltrim("{$char} foo bar {$char}"), Str::ltrim("{$char} foo bar {$char}"));
}
}

public function testRtrim()
Expand All @@ -902,6 +922,16 @@ public function testRtrim()
);

$this->assertSame(" \xE9", Str::rtrim(" \xE9 "));

$rtrimDefaultChars = [' ', "\n", "\r", "\t", "\v", "\0"];

foreach ($rtrimDefaultChars as $char) {
$this->assertSame('', Str::rtrim(" {$char} "));
$this->assertSame(rtrim(" {$char} "), Str::rtrim(" {$char} "));

$this->assertSame("{$char} foo bar", Str::rtrim("{$char} foo bar {$char}"));
$this->assertSame(rtrim("{$char} foo bar {$char}"), Str::rtrim("{$char} foo bar {$char}"));
}
}

public function testSquish()
Expand Down