Remove Duplicate Characters in Strings With Laravel

If you need to remove string duplication in Laravel and PHP applications, Laravel v11.20 adds a new method named deduplicate to the String helpers, which allows you to quickly and easily remove duplicate characters. use Illuminate\Support\{Str,Stringable}; $string = '/usr/local////path/to///desktop'; Str::deduplicate($string, '/'); // "/usr/local/path/to/desktop" (new Stringable($string))->deduplicate('/')->toString(); // "/usr/local/path/to/desktop" The default replacement character in the deduplicate() method is a space character, which could also be a use for the squish() helper introduced in Laravel v9.7.0. Let me illustrate when squish() might be a better choice to remove extra spaces: Str::deduplicate(' John Smith Jr. '); // " John Smith Jr. " Str::squish(' John Smith Jr. '); // "John Smith Jr." Note the extra space ' ' characters at the beginning and end using deduplicate(), which is by design. Deduplicate is used to replace consecutive insta

Remove Duplicate Characters in Strings With Laravel


Click the link to join the registration on WhatsApp: https://chat.whatsapp.com/KThkuZlWaTcCu3iJHGBmLM


Click the link to join the registration on WhatsApp: https://chat.whatsapp.com/KThkuZlWaTcCu3iJHGBmLM


Click the link to join the registration on WhatsApp: https://chat.whatsapp.com/KThkuZlWaTcCu3iJHGBmLM

If you need to remove string duplication in Laravel and PHP applications, Laravel v11.20 adds a new method named deduplicate to the String helpers, which allows you to quickly and easily remove duplicate characters.

use Illuminate\Support\{Str,Stringable};

$string = '/usr/local////path/to///desktop';

Str::deduplicate($string, '/');
// "/usr/local/path/to/desktop"

(new Stringable($string))->deduplicate('/')->toString();
// "/usr/local/path/to/desktop"

The default replacement character in the deduplicate() method is a space character, which could also be a use for the squish() helper introduced in Laravel v9.7.0. Let me illustrate when squish() might be a better choice to remove extra spaces:

Str::deduplicate(' John     Smith Jr.  '); // " John Smith Jr. "
Str::squish(' John     Smith Jr.  '); // "John Smith Jr."

Note the extra space ' ' characters at the beginning and end using deduplicate(), which is by design. Deduplicate is used to replace consecutive instances of a given character with a single character in the given string. Squish also removes other space characters, such as newlines and tabs:

// Each example returns `laravel php framework`:
Str::squish(' laravel php framework '));
Str::squish("laravel\t\tphp\n\nframework");
Str::squish('
    laravel
    php
    framework
');

The post Remove Duplicate Characters in Strings With Laravel appeared first on Laravel News.

Join the Laravel Newsletter to get all the latest Laravel articles like this directly in your inbox.

What's Your Reaction?

like

dislike

love

funny

angry

sad

wow

Keep advertising to get more people

You could reach thousands of more people for every ₦1,000 you spend. https://doacweb.com/advertising

Adverts on doacWeb can be informative, educative or persuasive in nature.

doacWeb Ads is always directed at a broad audience (reaching thousands of people day by day), not few individuals — it deliver your advert to the target audience at the same time. Putting your offer in front of the right people — who have the money and interest in what you sell.

doacWeb Advertising gives you advantage as adverts passes through https://doacweb.com to the internet, reaching millions of people over the internet, and to grow your audience.

Grow your business, Be known, Boost your visibility, Drive engagements, Get new customers and Increase sales.

doacWeb acts as a global advertising media, to let people — individuals — and businesses, to promote and reach more interested people.

Get Started.

WhatsApp: 09031633831

Email: info@doacweb.com
Keep advertising to get more people

You could reach thousands of more people for every ₦1,000 you spend. https://doacweb.com/advertising

Adverts on doacWeb can be informative, educative or persuasive in nature.

doacWeb Ads is always directed at a broad audience (reaching thousands of people day by day), not few individuals — it deliver your advert to the target audience at the same time. Putting your offer in front of the right people — who have the money and interest in what you sell.

doacWeb Advertising gives you advantage as adverts passes through https://doacweb.com to the internet, reaching millions of people over the internet, and to grow your audience.

Grow your business, Be known, Boost your visibility, Drive engagements, Get new customers and Increase sales.

doacWeb acts as a global advertising media, to let people — individuals — and businesses, to promote and reach more interested people.

Get Started.

WhatsApp: 09031633831

Email: info@doacweb.com
Keep advertising to get more people

You could reach thousands of more people for every ₦1,000 you spend. https://doacweb.com/advertising

Adverts on doacWeb can be informative, educative or persuasive in nature.

doacWeb Ads is always directed at a broad audience (reaching thousands of people day by day), not few individuals — it deliver your advert to the target audience at the same time. Putting your offer in front of the right people — who have the money and interest in what you sell.

doacWeb Advertising gives you advantage as adverts passes through https://doacweb.com to the internet, reaching millions of people over the internet, and to grow your audience.

Grow your business, Be known, Boost your visibility, Drive engagements, Get new customers and Increase sales.

doacWeb acts as a global advertising media, to let people — individuals — and businesses, to promote and reach more interested people.

Get Started.

WhatsApp: 09031633831

Email: info@doacweb.com