PHP stristr() Function: Syntax, Parameters, Return Value, and Examples
This article explains the PHP stristr() function—the case‑insensitive version of strstr—including its syntax, parameter details, return behavior, and three illustrative code examples demonstrating typical usage scenarios. It also clarifies the effect of the optional before_needle flag and shows how to handle cases where the needle is not found.
stristr() is the case‑insensitive version of strstr() in PHP.
Signature:
string stristr ( string $haystack , mixed $needle [, bool $before_needle = false ] )Description: Returns the portion of $haystack from the first occurrence of $needle to the end of the string, ignoring case. If $before_needle is true, the part before the needle is returned. If the needle is not found, FALSE is returned.
Parameters: $haystack – the string to search in. $needle – the string to search for; if not a string it is converted to an integer character code. $before_needle – optional boolean; when true, returns the segment before the needle (excluding the needle).
Return value: The matching substring, or FALSE if the needle is not found.
Example 1:
<?php
$email = '[email protected]';
echo stristr($email, 'e'); // Outputs [email protected]
echo stristr($email, 'e', true); // Outputs US (since PHP 5.3.0)
?>Example 2:
<?php
$string = 'Hello World!';
if (stristr($string, 'earth') === FALSE) {
echo '"earth" not found in string';
}
?>Example 3:
<?php
$string = 'APPLE';
echo stristr($string, 97); // 97 = ASCII for 'a', outputs APPLE
?>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.
