PHP strstr() Function: Usage, Parameters, Return Values, and Examples
This article explains PHP's strstr() function, detailing its purpose of locating the first occurrence of a needle in a haystack string, describing its parameters and return values, noting case‑sensitivity and performance considerations, and providing two practical code examples.
strstr() finds the first occurrence of a needle string within a haystack string and returns the portion of the haystack from that point to the end.
Parameters
haystack : the input string.
needle : the string to search for; if not a string it is converted to an integer and used as a character offset.
before_needle (optional, bool): when true, the function returns the part of haystack before the needle.
Return value
Returns the resulting substring on success, or FALSE if the needle is not found.
Notes
The function is case‑sensitive; use stristr() for case‑insensitive search.
For a simple existence check, strpos() is faster and uses less memory.
Example 1
<?php
$email = '[email protected]';
$domain = strstr($email, '@');
echo $domain; // prints @example.com
$user = strstr($email, '@', true);
echo $user; // prints name
?>Example 2
<?php
echo strstr("Hello world!", "world"); // outputs world!
?>Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Laravel Tech Community
Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
