PHP Laravel - How to Check if a String Contains a Specific Word?

By Hardik Savani | June 25, 2019 | | 9349 Viewer | Category : PHP 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.

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
My name is Hardik Savani. I'm a full-stack developer, entrepreneur and owner of Aatman Infotech. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.
Follow Me: Github Twitter
***Do you want me hire for your Project Work? Then Contact US.


We are Recommending you: