How to Check if a String Contains Another Substring in PHP

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.

  1. str_contains() which checks if a string contains a specific substring.
  2. str_end_width() which checks if a string end with a specific substring.
  3. str_starts_with() which checks if a string starts with a specific substring.

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():

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.

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:

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.

Leave a comment

Your email address will not be published.