One of the most common tasks in PHP programming is to determine whether a string contains a particular substring. This operation is essential for various applications, such as searching for keywords in user input, validating user data, or filtering strings based on certain conditions.
In this article, we will explore different techniques to check if a string contains a substring in PHP. We will cover functions like strpos(), strstr(), stripos(), and stristr() and explain their use-cases, advantages, and limitations.
1. Using strpos()
The strpos()
function is a popular and efficient method for checking if a string contains a substring. It returns the position of the first occurrence of the substring in the main string or FALSE if the substring is not found.
Syntax:
1 | strpos(string $haystack, string $needle, int $offset = 0): int|false |
Here’s an example of how to use strpos():
1 2 3 4 5 6 7 8 | $haystack = "Hello, welcome to the PHP world!"; $needle = "PHP"; if (strpos($haystack, $needle) !== false) { echo "Substring found!"; } else { echo "Substring not found!"; } |
2. Using strstr()
The strstr()
function returns the part of the main string starting from the first occurrence of the substring, or FALSE if the substring is not found. To check if a string contains a substring, you can simply check if the result is not FALSE.
Syntax:
1 | strstr(string $haystack, string $needle, bool $before_needle = false): string|false |
Here’s an example of how to use strstr():
1 2 3 4 5 6 7 8 | $haystack = "Hello, welcome to the PHP world!"; $needle = "PHP"; if (strstr($haystack, $needle) !== false) { echo "Substring found!"; } else { echo "Substring not found!"; } |
3. Using stripos()
The stripos()
function is similar to strpos()
, but it is case-insensitive. It returns the position of the first occurrence of the substring in the main string, regardless of the case, or FALSE if the substring is not found.
Syntax:
1 | stripos(string $haystack, string $needle, int $offset = 0): int|false |
Here’s an example of how to use stripos():
1 2 3 4 5 6 7 8 | $haystack = "Hello, welcome to the PHP world!"; $needle = "php"; if (stripos($haystack, $needle) !== false) { echo "Substring found!"; } else { echo "Substring not found!"; } |
4. Using stristr()
The stristr()
function is a case-insensitive version of strstr()
. It returns the part of the main string starting from the first occurrence of the substring, regardless of the case, or FALSE if the substring is not found.
Syntax:
1 | stristr(string $haystack, string $needle, bool $before_needle = false): string|false |
Here’s an example of how to use stristr():
1 2 3 4 5 6 7 8 | $haystack = "Hello, welcome to the PHP world!"; $needle = "php"; if (stristr($haystack, $needle) !== false) { echo "Substring found!"; } else { echo "Substring not found!"; } |
5. Using preg_match()
If you need more flexibility and want to search for substrings using regular expressions, you can use the preg_match()
function. It searches for a pattern in a given string and returns 1 if the pattern is found or 0 if it’s not found.
Syntax:
1 | preg_match(string $pattern, string $subject, array &$matches = null, int $flags = 0, int $offset = 0): int|false |
Here’s an example of how to use preg_match():
1 2 3 4 5 6 7 8 | $haystack = "Hello, welcome to the PHP world!"; $needle = "/php/i"; // Case-insensitive search if (preg_match($needle, $haystack)) { echo "Substring found!"; } else { echo "Substring not found!"; } |
6. Using mb_strpos()
For multibyte string support, you can use the mb_strpos()
function, which works similarly to strpos()
but supports a wider range of character encodings.
Syntax:
1 | mb_strpos(string $haystack, string $needle, int $offset = 0, string $encoding = null): int|false |
Here’s an example of how to use mb_strpos():
1 2 3 4 5 6 7 8 9 | $haystack = "Hello, welcome to the PHP world!"; $needle = "PHP"; $encoding = "UTF-8"; if (mb_strpos($haystack, $needle, 0, $encoding) !== false) { echo "Substring found!"; } else { echo "Substring not found!"; } |
7. Using mb_stripos()
The mb_stripos()
function is a case-insensitive version of mb_strpos()
and supports multibyte character encodings.
Syntax:
1 | mb_stripos(string $haystack, string $needle, int $offset = 0, string $encoding = null): int|false |
Here’s an example of how to use mb_stripos():
1 2 3 4 5 6 7 8 9 | $haystack = "Hello, welcome to the PHP world!"; $needle = "php"; $encoding = "UTF-8"; if (mb_stripos($haystack, $needle, 0, $encoding) !== false) { echo "Substring found!"; } else { echo "Substring not found!"; } |
Conclusion
In this article, we have covered seven different methods – strpos()
, strstr()
, stripos()
, stristr()
, preg_match()
, mb_strpos(), and mb_stripos()
– for checking if a string contains a substring in PHP. Depending on your application’s specific needs and character encoding requirements, you can choose the most suitable method. Remember to use strict comparisons (i.e., !== false) when checking the results of these functions, and consider using preg_match() if you need to search for substrings using regular expressions.
5 Comments
Hi, i have a rather twisted task similar to this post. If i want to check for sub strings in a string and want to put spaces and whole words to account, how do i do that? For example; the string “This is main string”, if i use it to check for the sub string “Th” it should not be true unless i put the whole word “This”, and if i want to get the phrase “is main” it should return true. How can i achieve that in php?
Just a test comment, but a nice article.
thanks for nice article
Example 3 should use === instead of ==.
If $str does NOT contain the substring ‘This‘, then strpos($str, ‘This’) will return false.
Therfore, strpos($str, ‘This’) == 0 evaluates to true
Thank you Kim, I have updated tutorial.