Laravel Tutorials

String Manipulation with Laravel's remove Method

Published
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

Sponsored

serpapi logo
SerpApi

The Web Search API for Your LLM and AI Applications

Visit SerpApi

The latest

View all →
PhpStorm 2026.3 Adds an Agent Workspace and PHP 8.6 Support image

PhpStorm 2026.3 Adds an Agent Workspace and PHP 8.6 Support

Read article
Insert Middleware Before or After Existing Laravel Middleware image

Insert Middleware Before or After Existing Laravel Middleware

Read article
Laravel Herd 2: Coding Agents, Workspaces, and Cloud Deploys image

Laravel Herd 2: Coding Agents, Workspaces, and Cloud Deploys

Read article
Strict Request Input Casting in Laravel image

Strict Request Input Casting in Laravel

Read article
Prevent Duplicate File Uploads With Laravel Storage Dedupler image

Prevent Duplicate File Uploads With Laravel Storage Dedupler

Read article
Slow Job Alerts and Replica Commit IDs in Laravel 13.40 image

Slow Job Alerts and Replica Commit IDs in Laravel 13.40

Read article