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
 

2 months ago

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

If you want to manage your VPS / VM Server without touching the command line go and  Checkout this linkServerAvatar allows you to quickly set up WordPress or Custom PHP websites on VPS / VM in a  matter of minutes.  You can host multiple websites on a single VPS / VM, configure SSL certificates, and monitor the health of your server without ever touching the command line interface.

Comments (0)

ServerAvatar
Multiple sites on a single VPS/VM/Server Deploy PHP sites on cloud in minutes, simple as that!
ServerAvatar
Generate Free SSL Certificate - SSLForWeb

Generate Free SSL Certificate

  • 90-Day Certificates
  • SSL Monitoring
  • Wildcard Certificates
  • Multiple Domains
  • Certificate Management
  • Expiration Reminders

Comment


Note: All Input Fields are required.