Laravel String Helper Function: is

November 16, 2016 —John Koster

The is helper method will indicate if the given $value matches the given $pattern. If the $value is the $pattern or if the $value matches the $pattern, the method will return true, otherwise it returns false.

The signature for the is helper method:

is($pattern, $value)

The following examples will return true:

1use Illuminate\Support\Str;
2 
3// true
4Str::is('word', 'word');
5 
6// true
7Str::is('pre*', 'prepare');
8 
9// true
10Str::is('*pare', 'prepare');
11 
12// true
13Str::is('*pare', 'compare'));
14 
15// true
16Str::is('m*e', 'mouse');
17 
18// true
19Str::is('m*e', 'meme');

The following example return false, and is just to demonstrate that the is method will not match traditional regular expressions:

1use Illuminate\Support\Str;
2 
3// false
4Str::is('[0-9]', '1');

However, any of these would return true:

1use Illuminate\Support\Str;
2 
3// true
4Str::is('[0-9]*', '[0-9] any thing');
5 
6// true
7Str::is('[0-9]*', '[0-9][a-z]');

#str_is($pattern, $value)

The str_is function is a shortcut to calling Str::is. This function is declared in the global namespace.

Some absolutely amazing
people

The following amazing people help support this site and my open source projects ♥️
If you're interesting in supporting my work and want to show up on this list, check out my GitHub Sponsors Profile.