Remove Duplicate Characters in Strings With Laravel
Laravel v11.20 introduces the deduplicate() method for removing duplicate characters from strings. This method takes two arguments: the string to be modified and the character to be removed. The default replacement character is a space. Squish(), introduced in Laravel v9.7.0, can also remove extra spaces. Deduplicate() replaces consecutive instances of a character with a single character, while Squish() removes any extra space characters, including newlines and tabs. Here are some examples of using these methods:- Str::deduplicate('/usr/local////path/to///desktop', '/') returns "/usr/local/path/to/desktop".
- (new Stringable('/usr/local////path/to///desktop'))->deduplicate('/')->toString() returns "/usr/local/path/to/desktop".
- Str::deduplicate(' John Smith Jr. ') returns " John Smith Jr. ", while Str::squish(' John Smith Jr. ') returns "John Smith Jr.".
- `Str::squish(' laravel php framework '), Str::squish("laravel\t\tphp\n\nframework"), and Str::squish('
laravel
php
framework
')` all return "laravel php framework".
deduplicate()method for removing duplicate characters from strings. This method takes two arguments: the string to be modified and the character to be removed. The default replacement character is a space. Squish(), introduced in Laravel v9.7.0, can also remove extra spaces. Deduplicate() replaces consecutive instances of a character with a single character, while Squish() removes any extra space characters, including newlines and tabs. Here are some examples of using these methods:-Str::deduplicate('/usr/local////path/to///desktop', '/')returns "/usr/local/path/to/desktop". -(new Stringable('/usr/local////path/to///desktop'))->deduplicate('/')->toString()returns "/usr/local/path/to/desktop". -Str::deduplicate(' John Smith Jr. ')returns " John Smith Jr. ", whileStr::squish(' John Smith Jr. ')returns "John Smith Jr.". - `Str::squish(' laravel php framework '), Str::squish("laravel\t\tphp\n\nframework"), and Str::squish(' laravel php framework ')` all return "laravel php framework".