|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()|
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.
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
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;
\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
!== 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;
Why to Use preg_match() And Not strpos()
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.
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:
- Best Way to Remove Public from URL in Laravel
- Error After php artisan config:cache In Laravel
- Specified Key Was Too Long Error In Laravel
- Active Directory Using LDAP in PHP or Laravel
- How To Use The Laravel Soft Delete
- How To Add Laravel Next Prev Pagination
- cURL error 60: SSL certificate problem: unable to get local issuer certificate
- Difference Between Factory And Seeders In Laravel
- Laravel: Increase Quantity If Product Already Exists In Cart
- How To Calculate Age From Birthdate
- 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!