W3docs

stristr()

The stristr() function in PHP is used to find the first occurrence of a substring in a string case-insensitively. It returns the portion of the string starting

Introduction

stristr() is a PHP string function that finds the first occurrence of a substring within a string, ignoring letter case, and returns everything from that point to the end of the string. If the substring isn't found, it returns false.

It is the case-insensitive counterpart of strstr(). Use stristr() when matching should treat Hello, HELLO, and hello as the same — for example searching user-typed keywords, parsing an email address, or splitting a string on a marker regardless of case.

This page covers the syntax, return value, the optional $before_needle switch, common pitfalls, and how stristr() differs from related functions.

Syntax

stristr(string $haystack, string $needle, bool $before_needle = false): string|false
ParameterDescription
$haystackThe string to search in.
$needleThe substring to search for. Matching is case-insensitive.
$before_needleOptional. If true, returns the part of $haystack before the first match instead of after it.

Return value: the matched portion of $haystack (its original case is preserved, not lowercased), or false if $needle does not appear.

Basic example

php— editable, runs on the server

Even though we searched for "BROWN" in uppercase, stristr() matched the lowercase brown in the string and returned the rest of the sentence starting from that word — with the original casing intact.

Getting the part before the match

Pass true as the third argument to return everything before the first match. This is handy for splitting a string on a delimiter, such as extracting the user portion of an email address:

<?php

$email = "[email protected]";

$after  = stristr($email, "@");        // from "@" onward
$before = stristr($email, "@", true);  // up to (not including) "@"

echo $after . "\n";   // @Example.com
echo $before . "\n";  // User

Checking whether a substring exists

A common mistake is to use stristr() as a boolean test. It works, but you must compare against false with the strict operator !==, because the returned string can itself be falsy (for example "0"):

<?php

$text = "Contact us at [email protected]";

if (stristr($text, "support") !== false) {
  echo "Contains 'support' (any case)";
} else {
  echo "Not found";
}
// Output: Contains 'support' (any case)

If you only need to know whether a substring exists — and don't need the matched portion — prefer stripos() (case-insensitive position) or strpos() (case-sensitive). They return the index of the match and are slightly faster because they don't copy the rest of the string.

FunctionCase-sensitive?Returns
stristr()NoMatched substring (or false)
strstr()YesMatched substring (or false)
stripos()NoPosition index (or false)
strpos()YesPosition index (or false)

Reach for stristr() when you want the text from the match onward and case should be ignored. Reach for stripos()/strpos() when you just want the position or a yes/no answer.

Notes and gotchas

  • false vs. empty string. A successful match never returns an empty string at the start, but a match at the very end of $haystack returns just the needle. Always test with !== false.
  • Case is ignored only for matching. The returned substring keeps the casing of the original $haystack.
  • Multibyte/UTF-8 text. stristr() operates on bytes. For reliable case-insensitive matching of accented or non-Latin characters, use mb_stristr() from the mbstring extension instead.
  • Non-string $needle. As of PHP 8.0, $needle is always treated as a string. In older versions a non-string needle was interpreted as its ASCII character code.

Conclusion

stristr() returns the portion of a string from the first case-insensitive match of a substring onward, or false when there is no match — and with $before_needle set to true, the portion before the match instead. Choose it when you need the matched text regardless of case; choose stripos() or strpos() when you only need a position or a presence check.

Practice

Practice
What does the 'stristr' function in PHP perform?
What does the 'stristr' function in PHP perform?
Was this page helpful?