Skip to content

Commit 4997554

Browse files
authored
Added Str trim methods (#6652)
1 parent 6cbd6f2 commit 4997554

File tree

3 files changed

+129
-4
lines changed

3 files changed

+129
-4
lines changed

src/Str.php

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1085,9 +1085,57 @@ public static function reverse($value): string
10851085
return implode(array_reverse(mb_str_split($value)));
10861086
}
10871087

1088+
/**
1089+
* Remove all whitespace from both ends of a string.
1090+
*
1091+
* @param string $value
1092+
* @param null|string $charlist
1093+
* @return string
1094+
*/
1095+
public static function trim($value, $charlist = null)
1096+
{
1097+
if ($charlist === null) {
1098+
return preg_replace('~^[\s\x{FEFF}\x{200B}\x{200E}]+|[\s\x{FEFF}\x{200B}\x{200E}]+$~u', '', $value) ?? trim($value);
1099+
}
1100+
1101+
return trim($value, $charlist);
1102+
}
1103+
1104+
/**
1105+
* Remove all whitespace from the beginning of a string.
1106+
*
1107+
* @param string $value
1108+
* @param null|string $charlist
1109+
* @return string
1110+
*/
1111+
public static function ltrim($value, $charlist = null)
1112+
{
1113+
if ($charlist === null) {
1114+
return preg_replace('~^[\s\x{FEFF}\x{200B}\x{200E}]+~u', '', $value) ?? ltrim($value);
1115+
}
1116+
1117+
return ltrim($value, $charlist);
1118+
}
1119+
1120+
/**
1121+
* Remove all whitespace from the end of a string.
1122+
*
1123+
* @param string $value
1124+
* @param null|string $charlist
1125+
* @return string
1126+
*/
1127+
public static function rtrim($value, $charlist = null)
1128+
{
1129+
if ($charlist === null) {
1130+
return preg_replace('~[\s\x{FEFF}\x{200B}\x{200E}]+$~u', '', $value) ?? rtrim($value);
1131+
}
1132+
1133+
return rtrim($value, $charlist);
1134+
}
1135+
10881136
public static function squish($value): null|array|string
10891137
{
1090-
return preg_replace('~(\s|\x{3164}|\x{1160})+~u', ' ', preg_replace('~^[\s\x{FEFF}]+|[\s\x{FEFF}]+$~u', '', $value));
1138+
return preg_replace('~(\s|\x{3164}|\x{1160})+~u', ' ', static::trim($value));
10911139
}
10921140

10931141
public static function substrReplace($string, $replace, $offset = 0, $length = null): array|string

src/Stringable.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -741,7 +741,7 @@ public function substrCount($needle, $offset = null, $length = null)
741741
*/
742742
public function trim($characters = null)
743743
{
744-
return new static(trim(...array_merge([$this->value], func_get_args())));
744+
return new static(Str::trim(...array_merge([$this->value], func_get_args())));
745745
}
746746

747747
/**
@@ -752,7 +752,7 @@ public function trim($characters = null)
752752
*/
753753
public function ltrim($characters = null)
754754
{
755-
return new static(ltrim(...array_merge([$this->value], func_get_args())));
755+
return new static(Str::ltrim(...array_merge([$this->value], func_get_args())));
756756
}
757757

758758
/**
@@ -763,7 +763,7 @@ public function ltrim($characters = null)
763763
*/
764764
public function rtrim($characters = null)
765765
{
766-
return new static(rtrim(...array_merge([$this->value], func_get_args())));
766+
return new static(Str::rtrim(...array_merge([$this->value], func_get_args())));
767767
}
768768

769769
/**

tests/StrTest.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -862,6 +862,83 @@ public function testReverse()
862862
}
863863
}
864864

865+
public function testTrim()
866+
{
867+
$this->assertSame('foo bar', Str::trim(' foo bar '));
868+
$this->assertSame('foo bar', Str::trim('foo bar '));
869+
$this->assertSame('foo bar', Str::trim(' foo bar'));
870+
$this->assertSame('foo bar', Str::trim('foo bar'));
871+
$this->assertSame(' foo bar ', Str::trim(' foo bar ', ''));
872+
$this->assertSame('foo bar', Str::trim(' foo bar ', ' '));
873+
$this->assertSame('foo bar', Str::trim('-foo bar_', '-_'));
874+
875+
$this->assertSame('foo bar', Str::trim(' foo bar '));
876+
877+
$this->assertSame('123', Str::trim(' 123 '));
878+
$this->assertSame('', Str::trim(''));
879+
$this->assertSame('', Str::trim(''));
880+
$this->assertSame('', Str::trim(''));
881+
$this->assertSame('', Str::trim(''));
882+
883+
$this->assertSame(
884+
'foo bar',
885+
Str::trim('
886+
foo bar
887+
')
888+
);
889+
$this->assertSame(
890+
'foo
891+
bar',
892+
Str::trim('
893+
foo
894+
bar
895+
')
896+
);
897+
898+
$this->assertSame("\xE9", Str::trim(" \xE9 "));
899+
}
900+
901+
public function testLtrim()
902+
{
903+
$this->assertSame('foo bar ', Str::ltrim(' foo bar '));
904+
905+
$this->assertSame('123 ', Str::ltrim(' 123 '));
906+
$this->assertSame('', Str::ltrim(''));
907+
$this->assertSame('', Str::ltrim(''));
908+
$this->assertSame('', Str::ltrim(''));
909+
$this->assertSame('', Str::ltrim(''));
910+
911+
$this->assertSame(
912+
'foo bar
913+
',
914+
Str::ltrim('
915+
foo bar
916+
')
917+
);
918+
$this->assertSame("\xE9 ", Str::ltrim(" \xE9 "));
919+
}
920+
921+
public function testRtrim()
922+
{
923+
$this->assertSame(' foo bar', Str::rtrim(' foo bar '));
924+
925+
$this->assertSame(' 123', Str::rtrim(' 123 '));
926+
$this->assertSame('', Str::rtrim(''));
927+
$this->assertSame('', Str::rtrim(''));
928+
$this->assertSame('', Str::rtrim(''));
929+
$this->assertSame('', Str::rtrim(''));
930+
931+
$this->assertSame(
932+
'
933+
foo bar',
934+
Str::rtrim('
935+
foo bar
936+
')
937+
);
938+
939+
$this->assertSame(" \xE9", Str::rtrim(" \xE9 "));
940+
}
941+
865942
public function testSquish()
866943
{
867944
$data = [

0 commit comments

Comments
 (0)