Home » Check If A String Contains A Specific Word In PHP

Check If A String Contains A Specific Word In PHP

Last updated on December 24, 2020 by

Table of Contents
1. Find Exact Match Using the PHP preg_match() Function
1.1 Case Insensitive Search with preg_match()
2. Use the PHP strpos() Function
3. Why to Use preg_match() And Not strpos()

Often, developers want to check string contains a specific word or not in PHP. For that, we can use strpos() and preg_match() functions to do that. Let’s see how to do it.

Check If a String Contains a Specific Word

01 Find Exact Match Using the PHP preg_match() Function

You can use the PHP preg_match() function to check whether a text contains a specific word or not.

Use: The preg_match() function is used to find the exact match of a pattern in a string of text using a regular expression search.

Return: This function will return true if the pattern is matched otherwise it will return false.

Example:

The following example will check for the exact match of the word like we are searching for aware then it will only search for the aware word and not a partial word like "beware", "unaware", "awareness", etc.

<?php 

/* The \b in the pattern indicates a word boundary, so only the distinct */

$string = "Oh! I am also not aware of this ";

if (preg_match("/\aware\b/", $string)) :
    echo "A match was found.";
else:
    echo "A match was not found.";
endif;

The \b expression in the pattern specify a word boundary.

Case Insensitive Search with preg_match()

For case-insensitive search put the "i" after the pattern delimiter as shown below:

<?php

$string = "Oh! I am also not AWARE of this ";

// The "i" after the pattern delimiter indicates a case-insensitive search

if (preg_match("/aware/i", $string)) :
    echo "A match was found.";
else:
    echo "A match was not found.";
endif;

In the above example, we are checking for aware specific word but our string contains a capital AWARE but although it will return A match was found. due to case-insensitive searches.

02 Use the PHP strpos() Function

You can also use the PHP’s strpos() function to check whether a string contains a specific word or not.

The strpos() function will return the position of the first occurrence of a substring in a string. It will return false if the match not found.

Please note that strpos() start searching from 0 index so use strict comparision operator using === or !== otherwise it will considered 0 = false.

Let’s see the example for better understanding:

<?php

$string = "I am not aware of this";

if ( strpos( $string, 'aware' ) !== false ) :
	echo 'true';
else:
	echo 'false';
endif;

You can also check if the string contains a word or not by using other PHP functions like stripos(), strstr(), stristr(), substr_count(), mb_strpos(), etc.

Why to Use preg_match() And Not strpos()

The preg_match() is better for word matching in comparison to strpos() because if you are checking the string contains a word like "are" and if your string contains words like careful, beware, prepare, etc. then strpos() will also return true.

To understand this situation please considered the below example:

<?php

$string = "I am not aware of this";

if ( strpos( $string, 'are' ) !== false ) :
	echo 'true';
else:
	echo 'false';
endif;

The above example should return the false, but it’s returning true as output.

Output:

true

That’s it for now. We hope this article helped you to check if a string contains a specific word in PHP.

Additionally, read our guide:

  1. Best Way to Remove Public from URL in Laravel
  2. Error After php artisan config:cache In Laravel
  3. Specified Key Was Too Long Error In Laravel
  4. Active Directory Using LDAP in PHP or Laravel
  5. How To Use The Laravel Soft Delete
  6. How To Add Laravel Next Prev Pagination
  7. cURL error 60: SSL certificate problem: unable to get local issuer certificate
  8. Difference Between Factory And Seeders In Laravel
  9. Laravel: Increase Quantity If Product Already Exists In Cart
  10. How To Calculate Age From Birthdate
  11. How To Find Duplicate Records in Database

Please let us know in the comments if everything worked as expected, your issues, or any questions. If you think this article saved your time & money, please do comment, share, like & subscribe. Thank you in advance 🙂. Keep Smiling! Happy Coding!

 
 

Leave a Comment