PHP: Check if a string contains a substring
- How to check if a string contains another string or substring in PHP
- Using str_contains() in PHP 8 and newer
- Using strpos() and stripos() for position-based checks
- Using preg_match() for word boundaries and pattern rules
- Using strstr() to return the matched substring onward
- Quick reference for choosing the right method
- Comprehensive example using all major methods
- Conclusion
How to check if a string contains another string or substring in PHP
Determining whether a string contains another string, substring, word, or character is a frequent requirement in PHP. In this blog post, I'll present the most reliable and modern ways to perform this task. Each method is concise, accurate, and based on current PHP best practices.
Using str_contains() in PHP 8 and newer
str_contains() is the simplest and most readable way to check if string contains substring or check if string contains word.
if (str_contains($haystack, $needle)) { echo "Found";} else { echo "Not found";}
Key points:
- Returns true if the substring exists, otherwise false.
- Ideal for most situations where you need to quickly check if string contains string.
- If
$needleis an empty string, the function always returns true. Validate$needleif that is not desired.
str_contains() is the recommended modern function and the clearest way to find in string operations.
Using strpos() and stripos() for position-based checks
For PHP versions before 8 or when you need the exact position of a match, use strpos().
if (strpos($haystack, $needle) !== false) { echo "Found";}
Important details:
- Returns the zero-based index of the match or false if not found.
- Always use
!== falsebecause position 0 is valid and would otherwise be misinterpreted. stripos()performs a case-insensitive search.
Use these functions when you must know where the substring occurs or when working on servers still running older PHP versions.
Using preg_match() for word boundaries and pattern rules
preg_match() is the tool of choice when you need to check if string contains word with precise matching requirements such as whole words, case-insensitive rules, optional characters, or advanced patterns.
if (preg_match('/example/', $string)) { echo "Found";}
Capabilities:
- Supports full regular expressions for complex searches.
- Add the
imodifier for case-insensitive matching. - Use boundaries to ensure accurate whole-word detection.
For example, to match the whole word joe but not joey:
if (preg_match('/\bjoe\b/i', $string)) { echo "Found whole word";}
Using strstr() to return the matched substring onward
If you need to retrieve the portion of the string beginning at the match, use strstr().
$result = strstr($string, $needle); if ($result !== false) { echo "Substring: " . $result;}
Characteristics:
- Returns the substring starting at the first occurrence of
$needle. - Returns false if not found.
- Useful when you want to capture all text that follows the matched segment.
Quick reference for choosing the right method
| Requirement | Best Method |
|---|---|
| Simple check in PHP 8 and newer | str_contains() |
| Need position or earlier PHP support | strpos() or stripos() |
| Need to check if string contains word with boundaries or regex rules | preg_match() |
| Need the substring starting from the match location | strstr() |
Comprehensive example using all major methods
$haystack = "The quick brown fox jumps over the lazy dog";$needle = "fox"; // 1. str_contains (PHP 8+)echo str_contains($haystack, $needle) ? "Found via str_contains\n" : "Not found\n"; // 2. strpos (case sensitive)if (strpos($haystack, $needle) !== false) { echo "Found via strpos\n";} // 3. stripos (case insensitive)if (stripos($haystack, $needle) !== false) { echo "Found via stripos\n";} // 4. preg_match (whole word match)if (preg_match('/\bfox\b/i', $haystack)) { echo "Found via preg_match (whole word)\n";} // 5. strstr to return substring from match$substring = strstr($haystack, $needle);if ($substring !== false) { echo "Found via strstr. Substring is: $substring\n";}
Conclusion
str_contains()is the clearest and most modern solution for checking if string contains substring in PHP 8 and newer.- Use
strpos()orstripos()when you need the match index or compatibility with older PHP versions. - Use
preg_match()for whole-word matching, complex patterns, or advanced rules. - Handle empty needles and case differences intentionally to avoid unexpected behavior.
Stay Updated.
I'll you email you as soon as new, fresh content is published.