Register Login

How to Check if String Contains Specific Word or Substring in PHP?

Updated Jul 27, 2020

Sometimes, you may want to check whether a string contains a specific word or substring. This is because you may have to perform some operation on that substring or word. One of the easiest ways to do this is in PHP is by using the strpos() function. We will look at the other ways to check for a substring in this article.

This tutorial will explain:

  1. How to check if string contains substring
  2. Find specific word in string

Using strrpos() Function to Check if String Contains Substring

The strrpos() method is used for determining the position of the last occurrence of a substring or word within a string. The string position starts at 0. If the string is not found, a Boolean value False is returned.

Syntax

strrpos(string,find,start)

The method takes 2 parameters, the string to search and the substring to look for. The optional parameter start tells where to begin the search process.

Example   

<?php
$word = "fox";
$mystring = "The quick brown fox jumps over the lazy dog";
// Check if string contains another string
$strposition  =  strpos($mystring, $word);
if($strposition === false){
    echo "Word Not Found!";
} else{
    echo "Word Fount at Position: ".$strposition;
}
?>

Output

Word Found at Position: 16

Example 2

<?php
$word = "fox";
$mystring = "The quick brown fox jumps over the lazy dog";
// Check if string contains another string
$strposition = strpos($mystring, $word);
if($strposition !== false){
    echo "Word Fount at Position: ".$strposition;
} else{
    echo "Word Not Found!";
}
?>

Output:

Word Found at Position: 16

In both of the above examples, the strpos() method takes the parameters $mystring and $word. The if condition checks whether the substring is found or not. The echo statement prints out the position of the string.   

Using preg_match to Search String Contains Substring

The preg_match() method is a regular expression match that helps in finding a string.

Syntax

preg_match(pattern, input)

The primary parameters are pattern that is the expression to search. The input parameter is string over which search takes place.    

<?php
$word = "fox";
$mystring = "The quick brown fox jumps over the lazy dog";
// Check if string contains another string
if (preg_match("/{$word}/", $mystring)) {
    echo 'Word Found';
}else{
echo "Word Not Found!";
}
?>

Output:

Word Found

If you consider the performance, strpos() is about three times faster than preg_match(). The preg_match() method takes around 1.5 seconds to finish an operation, whereas strpos() takes around 0.5 seconds.

Using preg_match function case-insensitive to search string contains substring

Example:

<?php
$word = "fox";
$mystring = "The quick brown fox jumps over the lazy dog";
// Check if string contains another string
if (preg_match("/{$word}/i", $mystring)) {
    echo 'Word Found';
}else{
echo "Word Not Found!";
}
?>

The i at the end of the regular expression is used for changing the regular expression to a case-insensitive one.

Using preg_match Function to Search Specific Work in String.

When you are looking for a specific word and not a substring, you need to understand that they can appear within other words too. For example,

  • The "are" at the beginning of "area"
  • The "are" in the middle of "fares"

One way to mitigate this would be to use a regular expression coupled with word boundaries (\b):

Example:

<?php
$word = "Are";
$mystring = "How you are of Traingle";
if (preg_match("/\b{$word}\b/i", $mystring)) {
    echo 'true';
}else{
echo 'false';
}
?>

The above example will search for the word “are”. It is case-insensitive.

Conclusion

If you want to search for the substring like url ‘https://www.stechies.com/photoshop/’ and you want to search for the substring ‘stechies’ then you should go for strpos().

But if you want to search for a specific word in the string like “stechies” in string ‘hi stechies is best site for training tutorials ’ then you should go for preg_match with regular expressions.


×