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.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
PHP stristr() Function: Usage, Parameters, Return Values, and Examples

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'
?>
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.

BackendPHPTutorialstristr
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.