PHP stristr() Function: Usage, Parameters, Return Values, and Examples
This article explains PHP's case‑insensitive stristr() function, detailing its parameters, return behavior, and providing three practical code examples that demonstrate extracting substrings, checking for needle existence, and using integer needles.
The stristr() function in PHP finds the first occurrence of a substring in a string, performing a case‑insensitive search.
Parameters :
haystack : the string to search in.
needle : the substring to look for; if not a string it is converted to an integer character code.
before_needle (optional, boolean): when true, returns the part of haystack before the found needle (excluding the needle).
Return value : the matched portion of the string starting from the first occurrence of needle to the end of haystack. If the needle is not found, the function returns FALSE.
Example 1 – case‑insensitive extraction:
<?php
$email = '[email protected]';
echo stristr($email, 'e'); // outputs [email protected]
echo stristr($email, 'e', true); // outputs US (PHP 5.3.0+)
?>Example 2 – checking existence:
<?php
$string = 'Hello World!';
if (stristr($string, 'earth') === FALSE) {
echo "\"earth\" not found in string";
}
?>Example 3 – using an integer as needle (ASCII code for 'a'):
<?php
$string = 'APPLE';
echo stristr($string, 97); // outputs APPLE because 97 = 'a'
?>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.
