Advertisement
  1. Code
  2. PHP
  3. PHP Scripts

How to Check if a String Contains Another Substring in PHP

Scroll to top

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 functions 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 the existence of an empty substring with them will always return true.

  1. str_contains() checks if a string contains a specific substring.
  2. str_ends_with() checks if a string ends with a specific substring.
  3. str_starts_with() checks if a string starts with a specific substring.
1
<?php
2
3
$sentence = "Dolphins and Elephants are intelligent animals.";
4
5
if(str_contains($sentence, "Elephants")) {
6
    echo 'Elephants are intelligent.';
7
}
8
// Elephants are intelligent.

9
10
if(str_starts_with($sentence, "Dolphins")) {
11
    echo 'Dolphins are intelligent.';
12
}
13
// Dolphins are intelligent.

14
15
if(str_ends_with($sentence, ".")) {
16
    echo 'There is a full stop at the end of the sentence.';
17
}
18
// There is a full stop at the end of the sentence.

19
20
?>

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 the 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 the first occurrence of a substring 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():

1
<?php
2
3
$sentence = "Amy, Angela, Adam and Andrew are going to a party.";
4
5
if(strpos($sentence, "Angela") != false) {
6
    echo 'Angela is going to a party!';
7
}
8
// Angela is going to a party!

9
10
11
if(strpos($sentence, "Amy") != false) {
12
    echo 'Amy is going to a party! (1)';
13
} else {
14
    echo 'Amy is not going to a party! (1)';
15
}
16
// Amy is not going to a party! (1)

17
18
if(strpos($sentence, "Amy") !== false) {
19
    echo 'Amy is going to a party! (2)';
20
} else {
21
    echo 'Amy is not going to a party! (2)';
22
}
23
// Amy is going to a party! (2)

24
25
?>

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 of 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 a 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.

1
<?php
2
3
$sentence = "Detect if this sentence mentions MaNGoEs.";
4
5
if(strpos($sentence, "mangoes") !== false) {
6
    echo 'There was no case-sensitive mango match!';
7
}
8
9
if(stripos($sentence, "mangoes") !== false) {
10
    echo 'There is a case-insensitive mango match!';
11
}
12
// There is a case-insensitive mango match!

13
14
?>

Using strstr() and stristr()

By default, the strstr() function returns a portion of the main string, starting from the substring and going to the end of the main string. It will return false if 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:

1
<?php
2
3
$sentence_a = "Is 984523850 your number?";
4
$sentence_b = "No, my number is 984523850";
5
6
if(strstr($sentence_a, "0") != false) {
7
    echo 'The first sentence has a 0 in it.';
8
}
9
// The first sentence has a 0 in it.

10
11
if(strstr($sentence_b, "0") != false) {
12
    echo 'The second sentence has a 0 in it. (1)';
13
} else {
14
    echo 'There is no zero in the second sentence. (1)';
15
}
16
// There is no zero in the second sentence. (1)

17
18
if(strstr($sentence_b, "0") !== false) {
19
    echo 'The second sentence has a 0 in it. (2)';
20
} else {
21
    echo 'There is no zero in the second sentence. (2)';
22
}
23
// The second sentence has a 0 in it. (2)

24
25
?>

You can use stristr() in place of strstr() to make the search case-insensitive. All other behavior of stristr() stays 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 the return value.

Advertisement
Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.