Laravel check if a string contains a specific word example

Laravel check if a string contains a specific word; In this tutorial you will learn how to check if a string contains any substring in laravel example.

Laravel predefined Str helper function has contains() method that will provide to check string contains in laravel application where in php we can use str_contains.

If Single Word Contains:

In laravel you can check like this:

In Laravel:

use Illuminate\Support\Str;
   
$string = 'This text is from condingdriver.com website.';
$word = 'condingdriver.com';
$contains = Str::contains($string, $word);
     
dd($contains); // true

In PHP:

In php we can use str_contains method just like below;

$string = 'This text is from condingdriver.com website.';
$word = 'condingdriver.com';
$contains = str_contains($string, $word); 

dd($contains); // true

If Multiple Words Contains:

In Laravel:

In laravel if multiple words check has in words you can use something like below:

use Illuminate\Support\Str;
  
$string = 'This text added is from codingdriver.com website.';
$firstWord = 'codingdriver.com';     
$secondWord = 'website';     
$contains = Str::contains($string, [$firstWord, $secondWord]);
     
dd($contains); // true

In php:

$string = 'This text added is from codingdriver.com website.';
$firstWord = 'codingdriver.com';     
$secondWord = 'website';     
$contains = str_contains($string, [$firstWord, $secondWord]);

dd($contains); // true

I hope you like these example;

Leave a Comment