Laravel has a Str
helper function. Str
class has contains()
method that will provide to check string contains in laravel application. contains()
take two arguments one should be a string and another will be a string or array.
How To Check if String Contains Specific Word in Laravel
Syntax
Str::contains($content, $find)
In the contains function, the First argument is a string that you want to check from. And the second argument is to check that specific word.
Example
use Illuminate\Support\Str;
$contains = Str::contains('Hello World', 'World');
// true
You can also pass an array of values to see if any of the values in the array are contained in the given string.
Syntax
Str::contains($content, [$firstWord, $secondWord])
Example
use Illuminate\Support\Str;
$contains = Str::contains('Hello World', ['Hello', 'Bar']);
// true
You can also pass an array of values to see if all values in the array are contained in the given string.
Example
use Illuminate\Support\Str;
$contains = Str::containsAll('Hello Laravel World', ['Hello', 'World']);
// true
Thank you for reading this article.
Read Also: Check If Folder Exists Before Creating Directory in Laravel