strstr()
strstr() — Find the first occurrence of a string.
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 part of haystack string starting from and including the first occurrence of needle to the end of haystack.
Signature
strstr(string $haystack, string $needle, bool $before_needle = false): string|false
Parameters
| Parameter | Type | What it takes |
|---|---|---|
| $haystack | string | The input string. |
| $needle | string | The string to search for. |
| $before_needle | bool | If true, strstr() returns the part of the haystack before the first occurrence of the needle (excluding the needle). |
Return value
Returns the portion of string, or false if needle is not found.
Example
A runnable snippet:
$email = 'name@example.com'; $domain = strstr($email, '@'); echo $domain, PHP_EOL; // prints @example.com $user = strstr($email, '@', true); echo $user, PHP_EOL; // prints name
Changelog
| Version | Change |
|---|---|
| 8.0.0 | needle now accepts an empty string. |
| 8.0.0 | Passing an int as needle is no longer supported. |
| 7.3.0 | Passing an int as needle has been deprecated. |
See also
stristr(), strrchr(), strpos(), strpbrk(), preg_match()
All PHP 8.5 built-in functions
Reference card built from the PHP manual page strstr() (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.