strstr
(PHP 4, PHP 5, PHP 7, PHP 8)
Find the first occurrence of a string
Description
Returns part of haystack string starting from and including the first occurrence of needle to the end of haystack.
This function is case-sensitive. For case-insensitive searches, use stristr().
If it is only required to determine if a particular needle occurs within haystack, the faster and less memory intensive str_contains() function should be used instead.
Parameters
haystackThe input string.
needleThe string to search for.
Non-string
before_needleIf true,
strstr()returns the part of thehaystackbefore the first occurrence of theneedle(excluding the needle).
Return Values
Returns the portion of string, or false if needle is not found.
Changelog
| Version | Description |
|---|---|
| 8.0.0 | Passing an Integer as needle is no longer supported. |
| 7.3.0 | Passing an Integer as needle has been deprecated. |
Examples
strstr() example
<?php
$email = 'name@example.com';
$domain = strstr($email, '@');
echo $domain, PHP_EOL; // prints @example.com
$user = strstr($email, '@', true);
echo $user, PHP_EOL; // prints name
?>