ItSolutionStuff.com

How to Check if String Contains Specific Word in Laravel?

By Hardik Savani • April 16, 2024
Laravel

This example will help you to check if a string contains any substring in laravel. you can check if url contains string in laravel using Str::contains(). i will give you simple example of laravel str contains case insensitive.

you can check if string contains a specific word in laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11.

Laravel has a Str helper function. Str class has contains() method that will provide to check string contains in laravel application. contains() take a two argument one should be string and another will be string or array.

I will give you both example so you can easily use in your controller method. so let's see bellow both example will help you to use.

With Single Word Contains:

use Illuminate\Support\Str;

$myString = 'This is from itsolutionstuff.com website.';

$contains = Str::contains($myString, 'itsolutionstuff.com');

// true

With Multiple Words Contains:

use Illuminate\Support\Str;

$myString = 'This is from itsolutionstuff.com website.';

$contains = Str::contains($myString, ['itsolutionstuff.com', 'website']);

// true

You can use easily with your controller method.

I hope it can help you....

Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Laravel Country List with Flags Example

Read Now →

How to Show Data in Modal using Ajax in Laravel?

Read Now →

How to Use and Install Font Awesome Icons in Laravel?

Read Now →

How to Install JQuery in Laravel Vite?

Read Now →

How to Use Limit and Offset in Laravel Eloquent?

Read Now →

Laravel Redirect to URL using redirect() Helper

Read Now →

Laravel Generate Slug from Title Example

Read Now →

How to Generate Random Unique String in Laravel?

Read Now →

Laravel Localization(trans Helper) Tutorial Example

Read Now →

How to Get Query Strings Value in Laravel?

Read Now →

How to Create Widgets in Laravel Application?

Read Now →

Laravel Change Date Format using Carbon Example

Read Now →

How to Create Custom Helper Function in Laravel?

Read Now →