How To Check if String Contains Specific Word in Laravel - Techvblogs

How To Check if String Contains Specific Word in Laravel

In this short article, We will learn How To Check if String Contains a Specific Word in Laravel.


Suresh Ramani - Author - Techvblogs
Suresh Ramani
 

1 day ago

TechvBlogs - Google News

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

Comments (0)

Comment


Note: All Input Fields are required.