stristr()

stristr() — Case-insensitive strstr().

Built-in function of PHP 8.5. The manual lists it as available in PHP 4, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.

What it does

Returns all of haystack starting from and including the first occurrence of needle to the end.

Signature

stristr(string $haystack, string $needle, bool $before_needle = false): string|false

Parameters

ParameterTypeWhat it takes
$haystackstringThe string to search in
$needlestringThe string to search for.
$before_needleboolIf true, stristr() returns the part of the haystack before the first occurrence of the needle (excluding needle).

Return value

Returns the matched substring. If needle is not found, returns false.

Example

A runnable snippet:

$email = 'USER@EXAMPLE.com'; echo stristr($email, 'e'), PHP_EOL; // outputs ER@EXAMPLE.com echo stristr($email, 'e', true), PHP_EOL; // outputs US

Changelog

VersionChange
8.2.0Case folding no longer depends on the locale set with setlocale(). Only ASCII case folding will be done. Non-ASCII bytes will be compared by their byte value.
8.0.0needle now accepts an empty string.
8.0.0Passing an int as needle is no longer supported.
7.3.0Passing an int as needle has been deprecated.

See also

strstr(), strrchr(), stripos(), strpbrk(), preg_match()

All PHP 8.5 built-in functions

Reference card built from the PHP manual page stristr() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.