• Reading time ~ 1 min
  • 05.06.2025

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('[email protected]', '[email protected]', 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.

Comments

No comments yet
Yurij Finiv

Yurij Finiv

Full stack

ABOUT

Professional Fullstack Developer with extensive experience in website and desktop application development. Proficient in a wide range of tools and technologies, including Bootstrap, Tailwind, HTML5, CSS3, PUG, JavaScript, Alpine.js, jQuery, PHP, MODX, and Node.js. Skilled in website development using Symfony, MODX, and Laravel. Experience: Contributed to the development and translation of MODX3 i...

About author CrazyBoy49z
WORK EXPERIENCE
Contact
Ukraine, Lutsk
+380979856297

We shoot down "Shahed" drones every day. Each one downed means lives saved. But we need mobility: a van or a trailer. Every donation = another night under protection.

🚐 Van fundraiser for my unit, 1020 regiment 🎯 Goal: 500,000 ₴
🔗 Donation link 💳 4441 1111 2546 4663