How to Check If a String Contains Multiple Specific Words in Laravel 9
In this lesson, we are going to see how to check If a string contains multiple specific words in Laravel 9, the containsAll helper method is used to check if a string contains multiple specific words.
The Str::containsAll helper method
The containsAll() method checks if the provided string contains multiple specific words if yes it returns true otherwise it returns false.
use Illuminate\Support\Str;
$sports = [
'soccer',
'tennis',
'volleyball',
];
Str::containsAll(
'soccer, tennis',
$sports
);
//true
The Str::containsAll helper method is case sensitive
You can disable the case-sensitive search by using the code below:
use Illuminate\Support\Str;
$sports = [
'soccer',
'tennis',
'volleyball',
];
Str::containsAll(
'SOCCER, tennis',
$sports,
true
);
//true