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.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
PHP stristr() Function: Syntax, Parameters, Return Value, and Examples

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
?>
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Backendphp-functionsstring-functionscase-insensitive-search
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.