Published in Laravel 5

Laravel String Pattern Matching - Laravel 5.5 String Helper Method: "is"

By 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.

#Signature

The signature of the is method is:

1public static function is(
2 $pattern,
3 $value
4 );

#Example Use

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]');

#Global str_is Helper Function

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