String Manipulation with Laravel's remove Method

Last updated on by

String Manipulation with Laravel's remove Method image

Laravel enhances string manipulation capabilities with the powerful Str::remove method. This utility function simplifies the process of removing unwanted characters from strings.

The implementation offers both case-sensitive and case-insensitive options:

use Illuminate\Support\Str;
 
$string = 'Peter Piper picked a peck of pickled peppers.';
$removed = Str::remove('e', $string);
// Result: "Ptr Pipr pickd a pck of pickld ppprs."
 
// Case-insensitive removal
$removed = Str::remove('P', $string, false);
// Result: "eter ier icked a eck of ickled eers."

Here's how to use it in a content formatting utility:

class ContentFormatter
{
/**
* Clean unnecessary whitespace and formatting
*/
public function normalizeWhitespace(string $content): string
{
// Remove extra whitespace characters
return Str::remove(["\r", "\t", "\x0B"], $content);
}
 
/**
* Format telephone numbers consistently
*/
public function formatTelephone(string $number): string
{
// Strip all non-numeric characters
$digits = Str::remove([' ', '-', '(', ')', '.', '+'], $number);
 
// Format consistently based on length
return match(strlen($digits)) {
10 => substr($digits, 0, 3) . '-' . substr($digits, 3, 3) . '-' . substr($digits, 6),
11 => '+' . substr($digits, 0, 1) . ' ' . substr($digits, 1, 3) . '-' . substr($digits, 4, 3) . '-' . substr($digits, 7),
default => $digits
};
}
 
/**
* Clean input for database slugs
*/
public function slugify(string $text): string
{
// Remove special characters and replace spaces with dashes
$text = Str::remove(['!', '?', ',', '.', '&', '%', '$', '#', '@'], $text);
return Str::slug($text);
}
}

Laravel's remove method provides a clean, expressive way to handle common string cleaning operations without resorting to complex regular expressions.

Harris Raftopoulos photo

Senior Software Engineer • Staff & Educator @ Laravel News • Co-organizer @ Laravel Greece Meetup

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

image
Tinkerwell

Enjoy coding and debugging in an editor designed for fast feedback and quick iterations. It's like a shell for your application – but with multi-line editing, code completion, and more.

Visit Tinkerwell
Laravel Cloud logo

Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Cloud
Acquaint Softtech logo

Acquaint Softtech

Acquaint Softtech offers AI-ready Laravel developers who onboard in 48 hours at $3000/Month with no lengthy sales process and a 100 percent money-back guarantee.

Acquaint Softtech
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
PhpStorm logo

PhpStorm

The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

PhpStorm
No Compromises logo

No Compromises

Joel and Aaron, the two seasoned devs from the No Compromises podcast, are now available to hire for your Laravel project. ⬧ Flat rate of $9500/mo. ⬧ No lengthy sales process. ⬧ No contracts. ⬧ 100% money back guarantee.

No Compromises
Harpoon: Next generation time tracking and invoicing logo

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing

The latest

View all →
RouteKey Model Attribute in Laravel 13.21 image

RouteKey Model Attribute in Laravel 13.21

Read article
Laravel Time Machine: A Request Lifecycle Profiler image

Laravel Time Machine: A Request Lifecycle Profiler

Read article
Laravel SMS Catcher: A Local Dashboard for SMS Notifications image

Laravel SMS Catcher: A Local Dashboard for SMS Notifications

Read article
The Analytics Stack for Laravel: Traffic, Revenue and Attribution in One Place image

The Analytics Stack for Laravel: Traffic, Revenue and Attribution in One Place

Read article
Scaffold Packages with the `laravel package` Command in Laravel Installer v5.31.0 image

Scaffold Packages with the `laravel package` Command in Laravel Installer v5.31.0

Read article
Vocalizer: Local Text-to-Speech for PHP image

Vocalizer: Local Text-to-Speech for PHP

Read article