How to check if a string contains a certain word/text in PHP
  John Mwaniki /   15 Dec 2021

How to check if a string contains a certain word/text in PHP

As a developer, you will most likely at some point want to check whether a string contains a specific substring (character, word, or phrase)... maybe when you want to perform a certain action based on whether the string does or doesn't contain the said substring.

This can be a simple task to do manually when the string is short. For long strings of text, this may take you so long. The best option will be to do it programmatically where the check takes milliseconds to complete.

In this article, we will cover the various ways in which you can check if a string contains a specific text using PHP and with aid of multiple examples.

Methods of checking if a string contains a specific text in PHP

1. Using the str_contains() function

The str_contains() function returns true if a string contains a given substring (character, text, or word) or false if it doesn't.

Syntax

str_contains(string, find)

Parameters

Parameter Requirement Description
string Required Specifies the string in which we want to check the text.
find Required Specify the text for which we want to look for.

Example

<?php
$str = "I love coding in php";
$text = "php";
if(str_contains($str, $text)){
  echo "The text ‘php’ exists in the string variable";
}
else{
  echo "The text does not exist in the string";
}

Output

The text ‘php’ exists in the string variable

Our check has confirmed that the string contains the text "php".

Note: This function is case-sensitive.

Note: This function was introduced in PHP version 8. If you try executing it in an earlier PHP version, it will throw the error below.

Fatal error: Uncaught Error: Call to undefined function str_contains() in /path/to/file/filename.php:4 Stack trace: #0 {main} thrown in /path/to/file/filename.php on line 4

You should use it ONLY when you are running PHP version 8 or later.

2. Using the strpos() function

The strpos() function returns the position of the first occurrence of a text inside a string. It returns false if the text is not found in the string.

Syntax

strpos(string,find,start)

Parameters

Parameter Requirement Description
string Required Specifies the string in which we want to check the text
find Required Specifies the text in which we want to find
start Optional Specifies the position within the string where we want to start the search for the text. If start is a negative number, it counts from the end of the string.

Example

<?php
$str = "I love coding in php";
echo strpos($str, "php");

Output

17

In our example above, we are checking the position of the first occurrence of text "php" within the string. This is the position of the first letter/character of the text (first p of "php") from the beginning of the string (spaces included).

The first "p" of the text php is the 18th character of the string. But counting of the string positions start at zero (0) and not 1. Therefore, the text "php" starts at position 17.

However, when checking if a text exists within a string or not, we are not so much interested in its position. All we want to know is if the strpos() function returns true or false. If the output is NOT false, then the string contains the text. Else, it doesn't.

Example

<?php
$str = "I love coding in php";
$text = "php";
if(strpos($str, $text) !== false){
  echo "The text ‘php’ exists in the string variable";
}
else{
  echo "The text does not exist in the string";
}

Output

The text ‘php’ exists in the string variable

Our check has confirmed that the string contains the text "php".

Note that I have used !== false operator. The === true operator would work perfectly as well.

Using != false or == true or even if(strpos( ... ) ) {} is not recommended. This is because strpos() function will return 0 if the text whose existence we are checking is at position 0 (is the first word or character) of the string. These checks would confuse 0 with false and return false when the string already contains the text.

The strpos() is case-sensitive! If you want to do case-insensitive checks, use stripos() instead. The two work the same except for the case sensitivity.

3. Using the explode() and in_array() functions

The PHP explode() function is used to break a string into an indexed array.

Syntax

explode(separator,string,limit)

Parameters

Parameter Requirement Description
separator Required Specifies where to break the string. This can be anything such as a space (" "), comma(,), fullstop(.), a letter, etc. This parameter cannot be an empty string.
string Required Specifies the string in which we want to break into an array.
limit Optional Specifies the number of array elements to return. If it is set to 0, the function returns an array with only one element. If it is set to greater than 0, the function returns an array with a maximum of limit element(s). If it is less than 0, the function returns an array except for the last -limit elements().

Example

<?php
$str = "I love coding in php";
$array = explode(" ", $str);
var_dump($array);

Output

array(5) { [0]=> string(1) "I" [1]=> string(4) "love" [2]=> string(6) "coding" [3]=> string(2) "in" [4]=> string(3) "php" }

The in_array() function is used to search for a specific value in an array. It returns true, if the value exists in the array, and false if it doesn't.

Syntax

in_array(search, array)

Parameters

Parameter Requirement Description
search Required Specifies the text to search for in an array.
array Required Specifies the array in which we are doing the search.

Example

<?php
$array = array("Peter", "Mary", "Khan", "Juan");
if(in_array("Mary", $array)){
  echo "The text ‘Mary’ exists in the array";
}
else{
  echo "Does not exist in the array";
}

Output

The text ‘Mary’ exists in the array

Combining explode() and in_array() functions

<?php
$str = "I love coding in php";
$array = explode(" ", $str);
if(in_array("php", $array)){
  echo "The text ‘php’ exists in the string";
}
else{
  echo "Does not exist in the string";
}

Output

The text ‘php’ exists in the string

This way, we were able to combine the explode() and in_array() functions to check if a text exists in a string.

4. Using strstr() or stristr() functions

The strstr() function searches for the first occurrence of a text in a string.

Syntax

strstr(string,search,before_search)

This function returns the rest of the string after the first occurrence of the search text.

Parameters

Parameter Requirement Description
string Required Specifies the string in which we want to check the text
search Required Specifies the text that we are searching for. If this parameter is a number, it will search for the character matching the ASCII value of the number
before_search Optional This is boolean value(true or false) whose default is "false". If set to "true", it returns the part of the string before the first occurrence of the search parameter.

Example

<?php
$str = "Hello World! My name is John. I love PHP";
echo strstr($str, "John");

Output

John. I love PHP

This gives us the output of our string after the first occurrence of the search text "John".

But since all we want in this case is to know if the string contains the search text, all we want to know is if the output is false or not. If the output is NOT false, then we know that the string contains the text. Else, we know it doesn't.

Example

<?php
$str = "Hello World! My name is John. I love PHP";
if(strstr($str, "John") !== false){
  echo "The text ‘John’ exists in the string";
}
else{
  echo "The text ‘John’ doesn’t exist in the string";
}

Output

The text ‘John’ exists in the string

Another way to confirm if a string contains text using this function is by checking the length of the returned string. If the length of the output string is more than 0, then definitely the string contains the text.

Example

<?php
$str = "Hello World! My name is John. I love PHP";
if(strlen(strstr($str, "John")) > 0){
  echo "The text ‘John’ exists in the string";
}
else{
  echo "The text ‘John’ doesn’t exist in the string";
}

Output

The text ‘John’ exists in the string

The strstr() is case-sensitive! If you want to do case-insensitive checks, use stristr() instead. The two works exactly the same except for the case sensitivity.

That's it!

It's my hope you followed along and found this article helpful.