diff --git a/src/Illuminate/Support/Str.php b/src/Illuminate/Support/Str.php index 44bbf7918ead..52867d89e473 100644 --- a/src/Illuminate/Support/Str.php +++ b/src/Illuminate/Support/Str.php @@ -341,6 +341,18 @@ public static function convertCase(string $string, int $mode = MB_CASE_FOLD, ?st return mb_convert_case($string, $mode, $encoding); } + /** + * Replace consecutive instances of a given character with a single character in the given string. + * + * @param string $string + * @param string $character + * @return string + */ + public static function deduplicate(string $string, string $character = ' ') + { + return preg_replace('/'.preg_quote($character, '/').'+/u', $character, $string); + } + /** * Determine if a given string ends with a given substring. * diff --git a/src/Illuminate/Support/Stringable.php b/src/Illuminate/Support/Stringable.php index be4389d523eb..6ae57e032066 100644 --- a/src/Illuminate/Support/Stringable.php +++ b/src/Illuminate/Support/Stringable.php @@ -235,6 +235,17 @@ public function convertCase(int $mode = MB_CASE_FOLD, ?string $encoding = 'UTF-8 return new static(Str::convertCase($this->value, $mode, $encoding)); } + /** + * Replace consecutive instances of a given character with a single character. + * + * @param string $character + * @return static + */ + public function deduplicate(string $character = ' ') + { + return new static(Str::deduplicate($this->value, $character)); + } + /** * Get the parent directory's path. * diff --git a/tests/Support/SupportStrTest.php b/tests/Support/SupportStrTest.php index 813dccdbd94b..0901ce0b3f8e 100755 --- a/tests/Support/SupportStrTest.php +++ b/tests/Support/SupportStrTest.php @@ -400,6 +400,14 @@ public function testConvertCase() Str::convertCase('Hello', -1); } + public function testDedup() + { + $this->assertSame(' laravel php framework ', Str::deduplicate(' laravel php framework ')); + $this->assertSame('what', Str::deduplicate('whaaat', 'a')); + $this->assertSame('/some/odd/path/', Str::deduplicate('/some//odd//path/', '/')); + $this->assertSame('ムだム', Str::deduplicate('ムだだム', 'だ')); + } + public function testParseCallback() { $this->assertEquals(['Class', 'method'], Str::parseCallback('Class@method')); diff --git a/tests/Support/SupportStringableTest.php b/tests/Support/SupportStringableTest.php index e7978bd2c4fc..e28dbddc536d 100644 --- a/tests/Support/SupportStringableTest.php +++ b/tests/Support/SupportStringableTest.php @@ -212,6 +212,14 @@ public function testWhenContainsAll() })); } + public function testDedup() + { + $this->assertSame(' laravel php framework ', (string) $this->stringable(' laravel php framework ')->deduplicate()); + $this->assertSame('what', (string) $this->stringable('whaaat')->deduplicate('a')); + $this->assertSame('/some/odd/path/', (string) $this->stringable('/some//odd//path/')->deduplicate('/')); + $this->assertSame('ムだム', (string) $this->stringable('ムだだム')->deduplicate('だ')); + } + public function testDirname() { $this->assertSame('/framework/tests', (string) $this->stringable('/framework/tests/Support')->dirname());