Laravel Tutorials

Enable Flexible Pattern Matching with Laravel's Case-Insensitive Str::is Method

Published
Enable Flexible Pattern Matching with Laravel's Case-Insensitive Str::is Method image

Laravel enhances string pattern matching by adding case-insensitive support to the Str::is method, providing more flexible and user-friendly string comparisons with wildcard patterns.

The enhanced Str::is method accepts a third parameter for case-insensitive matching:

use Illuminate\Support\Str;

// Simple string matching
Str::is('Admin', 'admin', true); // true

// Wildcard pattern matching
Str::is('*.jpg', 'photo.JPG', true); // true

// Prefix matching
Str::is('SKU123*', 'sku12345', true); // true

// Custom patterns
Str::is('prd-001*', 'PRD-001-XYZ', true); // true

// Email matching
Str::is('JOHN.DOE@example.com', 'john.doe@example.com', true); // true

This enhancement proves particularly valuable in content management systems:

class AssetManager
{
    protected array $imageFormats = ['jpg', 'png', 'gif', 'webp'];

    public function validateAsset(string $filename)
    {
        foreach ($this->imageFormats as $format) {
            if (Str::is("*.{$format}", $filename, true)) {
                return true;
            }
        }
        
        return false;
    }

    public function processMediaUploads(array $files)
    {
        return collect($files)->filter(function ($file) {
            // Match media-specific files (e.g., MEDIA-*.*)
            return Str::is("MEDIA-*.*", $file, true);
        });
    }

    public function categorizeAsset(string $filename)
    {
        $categories = [
            'thumbnail' => 'THUMB-*.*',
            'banner' => 'BNR-*.*',
            'logo' => 'LOGO-*.*'
        ];

        foreach ($categories as $type => $pattern) {
            if (Str::is($pattern, $filename, true)) {
                return $type;
            }
        }

        return 'general';
    }
}

The case-insensitive option in Str::is creates more robust pattern matching by eliminating case sensitivity concerns, making applications more user-friendly while maintaining the powerful wildcard functionality.

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 →
Load Eloquent Models From Form Request Input in Laravel image

Load Eloquent Models From Form Request Input in Laravel

Read article
Laravel FFmpeg 9: Encode Video and Stream HLS and DASH image

Laravel FFmpeg 9: Encode Video and Stream HLS and DASH

Read article
Migration Durations and Job Memory Usage in Laravel 13.45 image

Migration Durations and Job Memory Usage in Laravel 13.45

Read article
Laravel CLI: One Binary for New PHP and Rust Laravel Apps image

Laravel CLI: One Binary for New PHP and Rust Laravel Apps

Read article
Phactorial: Render LaTeX Math to SVG and PNG in PHP image

Phactorial: Render LaTeX Math to SVG and PNG in PHP

Read article
Laravel, reimagined image

Laravel, reimagined

Read article