Enable Flexible Pattern Matching with Laravel's Case-Insensitive Str::is Method
Last updated on by Harris Raftopoulos

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 matchingStr::is('Admin', 'admin', true); // true // Wildcard pattern matchingStr::is('*.jpg', 'photo.JPG', true); // true // Prefix matchingStr::is('SKU123*', 'sku12345', true); // true // Custom patternsStr::is('prd-001*', 'PRD-001-XYZ', true); // true // Email matchingStr::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.