Have you ever needed to check if a certain word or phrase is present in a larger string in your PHP code? Maybe you need to filter user input or perform a search on a website. Whatever the reason, it’s a common task in PHP programming.
In this tutorial, I’ll show you how to do this using two functions: strpos()
and strstr()
. Don’t worry if you’re not familiar with these functions yet — I’ll explain how to use them step by step and which one to go for.
We’ll keep things simple and easy to understand, so even if you’re new to PHP, you’ll be able to follow along. By the time you’re done reading, you’ll have good knowledge of how to check strings for substrings in PHP, and you’ll be ready to use this skill in your own code.
Checking If Strings Contain Substrings
In PHP, the two best ways to check if a string contains a substring are by using the built-in strpos()
and strstr()
functions.
Using the strpos() Function
The strpos()
function, short for “string position,” returns the position of the first occurrence of a substring within a string. If the substring is not found, it returns false
.
Here’s an example:
$string = "Hello, world!";
$substring = "world";
if (strpos($string, $substring) !== false) {
echo "The string contains the substring.";
} else {
echo "The string does not contain the substring.";
}
In the above code, we’re checking if the string $string
contains the substring $substring
. If strpos()
returns anything other than false
, it means that the substring is contained in the string. (The number returned stands for the substring’s position in the string.)
Using the strstr() Function
The strstr()
function, short for “string within string,” searches for the first occurrence of a substring in a string and returns the substring from that point on to the end of the string. If the substring is not found, it returns false
.
Here’s an example:
$string = "Hello, world!";
$substring = "world";
if (strstr($string, $substring)) {
echo "The string contains the substring.";
} else {
echo "The string does not contain the substring.";
}
In the above code, we’re checking if the string $string
contains the substring $substring
. Once again, if strstr()
returns anything other than false
, it means that the substring is present in the string.
Note that both of the functions we just went over are case-sensitive. If you want to perform a case-insensitive search, you can use stripos()
or stristr()
functions instead of strpos()
or strstr()
, which work in absolutely the same way, but without case sensitivity.
How to Choose
Now that we know about the strpos()
and strstr()
functions, you might be wondering which one you should use. The answer depends on your specific use case.
If you only need to check if a substring is present in a string, and you don’t need to know its position or extract it, then strstr()
is a good choice. This function returns the substring from the first occurrence to the end of the string, which is all you need if you only care about the presence of the substring.
On the other hand, if you need to know the position of the first occurrence of the substring in the string, or if you need to extract the substring itself, then strpos()
is the better choice. For example, if you have a string containing a URL and you need to extract the domain name, you could use strpos()
like this:
$url = "https://www.example.com/index.html";
$pos = strpos($url, "://") + 3;
$domain = substr($url, $pos, strpos($url, "/", $pos) - $pos);
echo "Domain name: " . $domain;
In this example, we use strpos()
to find the position of the first occurrence of “://” in the URL, and then use substr()
to extract the domain name from the URL.
So, to sum up, use strstr()
if you only need to check the presence of a substring, and use strpos()
if you need to know the position of the first occurrence of the substring or need to extract the substring itself.