Use str_contains($haystack, $needle) (PHP 8.0 and later). It returns true or false and is case-sensitive. For a case-insensitive check use stripos($haystack, $needle) !== false, or mb_stripos for non-ASCII text. On older PHP, write strpos($haystack, $needle) !== false: the !== matters, because a match at position 0 is falsy.
For years PHP had no "contains" function, so the classic answer was strpos, which returns the match position or false and catches out anyone who writes if (strpos(...)). PHP 8.0 added str_contains, and that is the right call today. The sections below cover it, case-insensitive and UTF-8 searches, the strpos trap, finding positions and counting matches, matching a whole word instead of any substring, and checking a string against a list of words, then a table to pick from. Each example runs on this page: hit Run, then edit the code and run it again.
1str_contains (PHP 8.0+)Recommended
str_contains takes the string to search (the haystack) and the text to look for (the needle) and returns a bool, so it drops straight into an if. It compares bytes exactly: upper and lower case are different, and a space in the needle has to be there in the haystack too. It works on UTF-8 text as long as you want an exact match. If your host still runs PHP 7, the last line shows the equivalent that works on every version.
<?php $text = 'How are you today?'; var_dump(str_contains($text, 'are')); var_dump(str_contains($text, 'you t')); var_dump(str_contains($text, 'Are')); // case-sensitive // Use it straight in an if if (str_contains($text, 'today')) { echo "Found 'today'\n"; } // An empty needle is always found var_dump(str_contains($text, '')); // Before PHP 8.0: strpos() !== false does the same job var_dump(strpos($text, 'are') !== false);
Output
'are' and 'you t' are found, while 'Are' gives bool(false) because of the capital A. The empty needle prints bool(true): every string contains the empty string, so check for an empty $needle yourself if it comes from user input. The last line, strpos(...) !== false, gives the same bool(true) and is what to write on PHP 7; libraries such as symfony/polyfill-php80 also define str_contains for old versions.
2Case-insensitive: stripos and mb_stripos
There is no str_icontains. The usual case-insensitive check is stripos, the case-insensitive strpos, compared with !== false. Lowercasing both sides and calling str_contains reads well too. Both only fold the ASCII letters A to Z, though, so for accented or other non-ASCII text use the multibyte versions from the mbstring extension: mb_stripos, or mb_strtolower with str_contains.
<?php $text = 'Welcome to PHP, the Hypertext Preprocessor'; // stripos ignores case; compare its result with !== false var_dump(stripos($text, 'php') !== false); var_dump(stripos($text, 'HYPERTEXT') !== false); // Lowercasing both sides with str_contains works too var_dump(str_contains(strtolower($text), strtolower('PREPROCESSOR'))); // Outside ASCII, stripos and strtolower only fold A-Z $city = 'ÉCOLE in ZÜRICH'; var_dump(stripos($city, 'école') !== false); var_dump(mb_stripos($city, 'école') !== false); var_dump(str_contains(mb_strtolower($city), 'zürich')); // stripos counts bytes, mb_stripos counts characters echo stripos($city, 'ZÜRICH'), ' ', mb_stripos($city, 'zürich'), "\n";
Output
The three ASCII checks all print bool(true). With accented letters, stripos misses 'école' in 'ÉCOLE in ZÜRICH' (bool(false)) because it does not know that É and é are the same letter; mb_stripos and mb_strtolower do. The last line shows the other difference: the match starts at byte 10 but at character 9, since É takes two bytes in UTF-8. Use the mb_ position when you pass it on to mb_substr.
3The strpos trap: a match at position 0
strpos returns the position of the first match as an int, or false when there is none. Positions start at 0, and in a plain if or a loose == comparison, 0 counts as false. So the most common pre-8.0 "contains" check is wrong exactly when the needle is at the start of the string, and you will still find it in older code.
<?php function containsBuggy(string $haystack, string $needle): bool { // Bug: a match at position 0 returns 0, and 0 is falsy if (strpos($haystack, $needle)) { return true; } return false; } function containsFixed(string $haystack, string $needle): bool { return strpos($haystack, $needle) !== false; } $text = 'apple pie'; var_dump(strpos($text, 'apple')); var_dump(strpos($text, 'pie')); var_dump(strpos($text, 'cake')); echo 'buggy: ', containsBuggy($text, 'apple') ? 'yes' : 'no', "\n"; echo 'fixed: ', containsFixed($text, 'apple') ? 'yes' : 'no', "\n"; // == false has the same bug, because 0 == false var_dump(strpos($text, 'apple') == false); var_dump(strpos($text, 'apple') === false);
Output
strpos finds 'apple' at int(0) and 'pie' at int(6), and returns bool(false) for 'cake'. The buggy helper turns the 0 into buggy: no even though the word is right there. The loose comparison strpos(...) == false is just as wrong (bool(true)); only the strict === or !== tells 0 and false apart. With str_contains the problem cannot happen, which is the main reason to prefer it.
4Finding the position and counting matches
When you need more than yes or no, use strpos for the first match, strrpos for the last, and the optional third argument, an offset, to search from a later point. substr_count counts how many times the needle occurs. To collect every position, loop with strpos and move the offset past each match.
<?php $text = 'the cat sat on the mat with the hat'; // Where is the first and the last match? var_dump(strpos($text, 'the')); var_dump(strrpos($text, 'the')); // Start searching after the first match with the offset argument var_dump(strpos($text, 'the', 1)); // How many times does it occur? echo substr_count($text, 'the'), "\n"; echo substr_count($text, 'at'), "\n"; // Every position, with an offset loop $positions = []; $offset = 0; while (($pos = strpos($text, 'at', $offset)) !== false) { $positions[] = $pos; $offset = $pos + 1; } echo implode(', ', $positions), "\n"; // substr_count does not count overlapping matches echo substr_count('aaaa', 'aa'), "\n"; // No case-insensitive substr_count: lowercase both sides echo substr_count(strtolower('The THE the'), 'the'), "\n";
Output
The first 'the' is at 0 and the last at 28, and with an offset of 1 the next one is at 15. substr_count finds 'the' 3 times and 'at' 4 times, inside cat, sat, mat and hat, at positions 5, 9, 20, 33. It skips overlaps, so 'aa' occurs 2 times in 'aaaa', not 3. Like strpos, it is case-sensitive; lowercase first for a count that ignores case. All of these count bytes; mb_strpos and mb_substr_count are the character-based versions.
5Matching a whole word with preg_match
"Contains a word" and "contains a substring" are not the same question. A substring check for cat is also true for concatenate, cats and bobcat. To match the word on its own, use a regular expression with \b, a word boundary: the point between a word character (letter, digit or underscore) and anything else. Add i to ignore case, u for UTF-8 text, and pass a word that comes from a variable through preg_quote so characters like . or + are taken literally.
<?php
$texts = [
'The cat sleeps.',
'Please concatenate the strings.',
'cats are great',
'Watch the bobcat',
'CAT scan results',
];
foreach ($texts as $text) {
$sub = stripos($text, 'cat') !== false ? 'yes' : 'no';
$word = preg_match('/\bcat\b/i', $text) === 1 ? 'yes' : 'no';
printf("%-32s substring: %-3s word: %s\n", $text, $sub, $word);
}
// A word from a variable: escape it, and use /u for UTF-8 text.
// (?<!\w) and (?!\w) also work when the word ends in a symbol.
function containsWord(string $text, string $word): bool
{
$pattern = '/(?<!\w)' . preg_quote($word, '/') . '(?!\w)/iu';
return preg_match($pattern, $text) === 1;
}
var_dump(containsWord('Is 3.5 the price?', '3.5'));
var_dump(containsWord('Is 315 the price?', '3.5'));
var_dump(containsWord('Un café, por favor', 'café'));
var_dump(containsWord('Dos cafés, por favor', 'café'));
var_dump(containsWord('I write C++ daily', 'C++'));
var_dump(preg_match('/\bC\+\+\b/', 'I write C++ daily'));
Output
All five lines contain the substring, but only The cat sleeps. and CAT scan results contain the word. preg_match returns 1 for a match and 0 for none (and false on a bad pattern), so compare with === 1. Thanks to preg_quote, 3.5 no longer matches 315. The helper uses (?<!\w) and (?!\w) instead of \b because \b needs a word character on its side: the plain \bC\+\+\b pattern prints int(0) on the last line, while the helper finds C++. Leave off u and accented words go wrong both ways: without it, /\bcafé\b/i misses café, and matches cafés.
6Any or all of several words
To check a string against a list, PHP 8.4 added array_any and array_all: each takes an array and a callback and stops as soon as the answer is known. array_filter tells you which words matched. On PHP 8.0 to 8.3, a short foreach that returns on the first hit does the same job, and a regex with alternation (error|failed|timeout) handles "any of these whole words" in one call.
<?php $text = 'Deploy failed: disk full on web-2'; $words = ['error', 'failed', 'disk']; // PHP 8.4: array_any and array_all $any = array_any($words, fn(string $w): bool => str_contains($text, $w)); $all = array_all($words, fn(string $w): bool => str_contains($text, $w)); var_dump($any, $all); // Which ones matched? $found = array_filter($words, fn(string $w): bool => str_contains($text, $w)); echo implode(', ', $found), "\n"; // Before PHP 8.4: a loop that stops at the first match function containsAny(string $haystack, array $needles): bool { foreach ($needles as $needle) { if (str_contains($haystack, $needle)) { return true; } } return false; } var_dump(containsAny($text, ['error', 'warning'])); var_dump(containsAny($text, ['full', 'missing'])); // One regex with alternation also answers "any of these words" $pattern = '/\b(?:error|failed|timeout)\b/i'; if (preg_match($pattern, $text, $m) === 1) { echo "first match: {$m[0]}\n"; }
Output
Two of the three words are in the text, so array_any gives bool(true) and array_all gives bool(false); array_filter lists failed, disk. The loop version returns bool(false) for error and warning and bool(true) as soon as it reaches full. For a case-insensitive list, swap str_contains for stripos(...) !== false in the callback. The regex reports the first word it found, first match: failed; if the words come from a variable, run each through preg_quote before joining them with |.
7Which should you use?
| Method | Returns | Case | Best for |
|---|---|---|---|
| str_contains($h, $n) | bool | Sensitive | Most checks on PHP 8.0+ |
| stripos($h, $n) !== false | int or false | Ignores ASCII case | Case-insensitive ASCII text |
| mb_stripos($h, $n) !== false | int or false | Ignores Unicode case | Case-insensitive UTF-8 text |
| strpos($h, $n) !== false | int or false | Sensitive | PHP 7; never without !== |
| substr_count($h, $n) | int | Sensitive | How many times it occurs |
| preg_match('/\bword\b/iu', $h) | 1, 0 or false | Your choice (i flag) | Whole words, patterns |
| array_any($words, ...) | bool | Depends on callback | Any or all of a list (PHP 8.4+) |
Frequently asked questions
Why does my strpos check fail when the word is at the start of the string?
Because strpos returns the position, and a match at the very start is position 0, which PHP treats as false in an if and in a loose == false comparison. Compare strictly: strpos($haystack, $needle) !== false. On PHP 8.0 and later, use str_contains($haystack, $needle), which returns a plain true or false and avoids the problem.
How do I check if a string contains a word case-insensitively in PHP?
Use stripos($haystack, $needle) !== false, or str_contains(strtolower($haystack), strtolower($needle)). Both only fold the ASCII letters A to Z. For accented or other non-ASCII text, use mb_stripos($haystack, $needle) !== false or str_contains(mb_strtolower($haystack), mb_strtolower($needle)). There is no built-in str_icontains.
How do I use str_contains on PHP 7?
str_contains was added in PHP 8.0. On PHP 7, write strpos($haystack, $needle) !== false, which gives the same result for a non-empty needle, or install the symfony/polyfill-php80 package, which defines str_contains when it is missing. Don't declare your own global str_contains without a function_exists check, or the code breaks once the host upgrades.
Why does str_contains return true for an empty string?
Every string contains the empty string, so str_contains($text, '') is always true, even when $text is empty too. strpos($text, '') likewise returns 0 on PHP 8.0 and later. If the needle comes from user input, check $needle !== '' before searching.
How do I check that a string contains a whole word and not just part of one?
Use a regex with word boundaries: preg_match('/\bcat\b/i', $text) === 1 matches "the cat" but not "concatenate" or "cats". When the word is in a variable, escape it with preg_quote($word, '/') and add the u flag for UTF-8 text. For words that start or end with a symbol, such as C++, use (?<!\w) and (?!\w) instead of \b.
Why do I get "Passing null to parameter #1 ($haystack) of type string is deprecated"?
Since PHP 8.1, passing null to a string parameter of a built-in function prints a deprecation, for example Deprecated: str_contains(): Passing null to parameter #1 ($haystack) of type string is deprecated. It usually means a value you expected to be a string is missing. Fix the source, or default it: str_contains($text ?? '', $needle).
How do I check if a string contains any of several words?
On PHP 8.4 and later, use array_any($words, fn($w) => str_contains($text, $w)); array_all checks that every word is present. On older versions, loop over the words with foreach and return true on the first str_contains hit. For whole words, one regex with alternation works too: preg_match('/\b(?:error|failed)\b/i', $text).