A lot of times when I am working with strings in PHP, it becomes important to check if a string contains another substring. PHP has a lot of functions to help you manipulate strings any way you like. We will be using some of these functions today to learn how to check if a string contains a specific substring.
Using New Functions in PHP 8
Three new function have been added in PHP 8 to help us figure out if a string contains another substring. Please keep in mind that all these functions are case sensitive and checking for existence of an empty substring with them will always return true
.
-
str_contains()
which checks if a string contains a specific substring. -
str_end_width()
which checks if a string end with a specific substring. -
str_starts_with()
which checks if a string starts with a specific substring.
<?php $sentence = "Dolphins and Elephants are intelligent animals."; if(str_contains($sentence, "Elephants")) { echo 'Elephants are intelligent.'; } // Elephants are intelligent. if(str_starts_with($sentence, "Dolphins")) { echo 'Dolphins are intelligent.'; } // Dolphins are intelligent. if(str_ends_with($sentence, ".")) { echo 'There is a full stop at the end of the sentence.'; } // There is a full stop at the end of the sentence. ?>
As I mentioned earlier, these functions perform a case sensitive match. You can also perform a case-insensitive match with them by first converting both the string and substring to same case using strtolower()
or strtoupper()
.
These functions are ideal if you don’t want to get any extra information about the substrings like their position. People who need extra information about the match should consider using the functions below.
Using strpos()
and stripos()
You can use the PHP strpos()
function to get the position of first occurrence of a subtring in a string. The function will return false
if it cannot find the substring.
When you use strpos()
to check for a substring make sure that you use the strict inequality operator. This is because the position returned by strpos()
starts with 0 and 0 can also equate to false
. Using a regular equality check will give you false negatives if the substring is right at the beginning of the main string.
Here are some examples of strpos()
:
<?php $sentence = "Amy, Angela, Adam and Andrew are going to a party."; if(strpos($sentence, "Angela") != false) { echo 'Angela is going to a party!'; } // Angela is going to a party! if(strpos($sentence, "Amy") != false) { echo 'Amy is going to a party! (1)'; } else { echo 'Amy is not going to a party! (1)'; } // Amy is not going to a party! (1) if(strpos($sentence, "Amy") !== false) { echo 'Amy is going to a party! (2)'; } else { echo 'Amy is not going to a party! (2)'; } // Amy is going to a party! (2) ?>
As you can see in the above example, Angela is not at the beginning of our sentence so it would have a non-zero position which evaluates to true
.
On the other hand, Amy is right at the beginning and its position 0 evaluates to false even though it exists in the string. Therefore, always make sure that you evaluate the value of strpos()
with !==
because there is no way to know where a substring might occur when dealing with strings in real life.
Sometimes, you might need to make this check of substring in a string case-insensitive. In that case, you can simply use the stripos()
function in PHP. It works exactly like strpos()
but makes the search case-insensitive.
<?php $sentence = "Detect if this sentence mentions MaNGoEs."; if(strpos($sentence, "mangoes") !== false) { echo 'There was no case-sensitive mango match!'; } if(stripos($sentence, "mangoes") !== false) { echo 'There is a case-insensitive mango match!'; } // There is a case-insensitive mango match! ?>
Using strstr()
and stristr()
By default, the strstr()
function returns a portion of the main string starting from the substring until the end of main string. It will return false
if the the substring does not exist inside the main string.
We can use this information to check the existence of a substring inside a string. All we have to do is evaluate the value returned by strstr()
and check if it is false
or not. Just like last time we will be using the strict inequality operator !==
to do the check.
Here are some examples:
<?php $sentence_a = "Is 984523850 your number?"; $sentence_b = "No, my number is 984523850"; if(strstr($sentence_a, "0") != false) { echo 'The first sentence has a 0 in it.'; } // The first sentence has a 0 in it. if(strstr($sentence_b, "0") != false) { echo 'The second sentence has a 0 in it. (1)'; } else { echo 'There is no zero in the second sentence. (1)'; } // There is no zero in the second sentence. (1) if(strstr($sentence_b, "0") !== false) { echo 'The second sentence has a 0 in it. (2)'; } else { echo 'There is no zero in the second sentence. (2)'; } // The second sentence has a 0 in it. (2) ?>
You can use stristr()
in place of strstr()
to make the search case-insensitive. All other behavior of stristr()
stays exactly the same.
Final Thoughts
In this quick tip, we went over three different ways of checking if a string contains a substring in PHP. You can use any of these functions depending on what information you need from the string. Using the new PHP 8 functions will allow you to skip any type of strict checking but you will need to use the older functions if you want to know the position of the substring or get a part of the main string as return value.