Skip to content

Commit 88663c1

Browse files
[9.x] Add String::squish() helper (#41791)
* Add squish helper * Additional test case * Add squish to Stringable * Fix code formatting * Make more consistent * formatting Co-authored-by: Taylor Otwell <[email protected]>
1 parent 7c9430a commit 88663c1

File tree

4 files changed

+43
-0
lines changed

4 files changed

+43
-0
lines changed

src/Illuminate/Support/Str.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -881,6 +881,17 @@ public static function snake($value, $delimiter = '_')
881881
return static::$snakeCache[$key][$delimiter] = $value;
882882
}
883883

884+
/**
885+
* Remove all "extra" blank space from the given string.
886+
*
887+
* @param string $value
888+
* @return string
889+
*/
890+
public static function squish($value)
891+
{
892+
return preg_replace('/\s+/', ' ', trim($value));
893+
}
894+
884895
/**
885896
* Determine if a given string starts with a given substring.
886897
*

src/Illuminate/Support/Stringable.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,16 @@ public function scan($format)
624624
return collect(sscanf($this->value, $format));
625625
}
626626

627+
/**
628+
* Remove all "extra" blank space from the given string.
629+
*
630+
* @return static
631+
*/
632+
public function squish()
633+
{
634+
return new static(Str::squish($this->value));
635+
}
636+
627637
/**
628638
* Begin a string with a single instance of a given value.
629639
*

tests/Support/SupportStrTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,17 @@ public function testSnake()
538538
$this->assertSame('żółtałódka', Str::snake('ŻółtaŁódka'));
539539
}
540540

541+
public function testSquish()
542+
{
543+
$this->assertSame('laravel php framework', Str::squish(' laravel php framework '));
544+
$this->assertSame('laravel php framework', Str::squish("laravel\t\tphp\n\nframework"));
545+
$this->assertSame('laravel php framework', Str::squish('
546+
laravel
547+
php
548+
framework
549+
'));
550+
}
551+
541552
public function testStudly()
542553
{
543554
$this->assertSame('LaravelPHPFramework', Str::studly('laravel_p_h_p_framework'));

tests/Support/SupportStringableTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,17 @@ public function testSlug()
611611
$this->assertSame('', (string) $this->stringable('')->slug());
612612
}
613613

614+
public function testSquish()
615+
{
616+
$this->assertSame('words with spaces', (string) $this->stringable(' words with spaces ')->squish());
617+
$this->assertSame('words with spaces', (string) $this->stringable("words\t\twith\n\nspaces")->squish());
618+
$this->assertSame('words with spaces', (string) $this->stringable('
619+
words
620+
with
621+
spaces
622+
')->squish());
623+
}
624+
614625
public function testStart()
615626
{
616627
$this->assertSame('/test/string', (string) $this->stringable('test/string')->start('/'));

0 commit comments

Comments
 (0)