Search for a string inside another string in PHP


- Checking if a string contains another string or substring in PHP
- str_contains(): PHP 8+ (Simplest and Most Readable)
- strpos() (and stripos()): Widely Supported Alternative
- preg_match(): When You Need Pattern Power
- strstr(): Simpler Alternative to Locate and Return the Remaining String
- Choosing the Right Tool: At a Glance
- Example: Testing Multiple Methods
- Final Thoughts
Checking if a string contains another string or substring in PHP
When working with PHP, determining whether a string contains another string or substring is a common and foundational task. In this post, we’ll explore several reliable methods each with their own benefits so you can choose the one that fits your needs best.
str_contains()
: PHP 8+ (Simplest and Most Readable)
Starting with PHP 8, str_contains()
is the most straightforward way to check if one string appears within another:
if (str_contains($haystack, $needle)) { echo "Found!";} else { echo "Not found.";}
- Returns
true
if$needle
exists within$haystack
, otherwisefalse
. - Clean syntax and readability.
-
Important caveat: If
$needle
is an empty string,str_contains()
always returnstrue
so ensure you validate that$needle
isn’t blank if that’s not desired.
strpos()
(and stripos()
): Widely Supported Alternative
For earlier PHP versions (pre‑8) or for more control over how matches are detected, strpos()
is a classic approach:
if (strpos($haystack, $needle) !== false) { echo "Found!";}
- Returns the 0‑based index of the first occurrence if found, or
false
if not. -
Important: Must compare with
!== false
, not just truthiness, because 0 is a valid position. -
stripos()
is the case‑insensitive version, returning the same type of result.
preg_match()
: When You Need Pattern Power
If you need more sophisticated matching like full‑word boundaries or complex patterns preg_match()
with regular expressions is your go-to:
if (preg_match('/example/', $string)) { echo "Found!";}
- Supports regex, so you can handle edge cases like matching whole words only, case‑insensitively, and more.
- Add the
i
flag for case‑insensitive search.
For instance, to match the whole word "joe" and avoid "joey", a regex like \bjoe\b
works perfectly.
strstr()
: Simpler Alternative to Locate and Return the Remaining String
If all you need is part of the string from the match onward, strstr()
is a neat choice:
$result = strstr($string, $needle);if ($result !== false) { // $result contains substring from $needle to end}
- Returns the matched part and onwards, or
false
if not found.
Choosing the Right Tool: At a Glance
Requirement | Recommended Method |
---|---|
PHP 8+, simple check | str_contains() |
Pre-PHP 8 or need position | strpos() / stripos() |
Whole word, patterns, or case‑insensitive | preg_match() |
Need substring starting from match | strstr() |
Example: Testing Multiple Methods
Here’s a quick example showcasing each method:
$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, strtoupper($needle)) !== false) { echo "Found via stripos\n";} // 4. preg_match (whole word, case‑insensitive)if (preg_match('/\\bfox\\b/i', $haystack)) { echo "Found via preg_match (whole word)\n";} // 5. strstrif (strstr($haystack, $needle) !== false) { echo "Found via strstr. Substring is: " . strstr($haystack, $needle);}
Final Thoughts
- The modern, cleanest option is
str_contains()
, ideal when using PHP 8 or newer. - For compatibility or when you need position data,
strpos()
remains reliable. - Don’t hesitate to reach for
preg_match()
when you need full regex control or whole‑word matching. - Always handle edge cases like empty needles or case differences wisely.
Stay Updated.
I'll you email you as soon as new, fresh content is published.