stristr() Function in PHP With Example

Support PHP Version: PHP 7.1, PHP 7.2, PHP 7.3, PHP 7.4, PHP 8.0, PHP 8.1, PHP 8.2, PHP 8.3 With Latest All Version Support.

Hello Friends Today, through this tutorial, I will tell you How to Use `stristr()` function using PHP, PHP 8, PHP 8.1, PHP 8.2 With Example. In PHP, the `stristr()` function is used to perform a case-insensitive search for a substring within another string. This function returns the part of the haystack (the string being searched) from the first occurrence of needle (the substring being searched for) to the end of the haystack, or FALSE if needle is not found.

Here’s the syntax for the `stristr()` function:

<?php
stristr(string $haystack, string $needle, bool $before_needle = false): string|false
?>

Parameters:
1. `$haystack`: The string to search in.
2. `$needle`: The substring to search for.
3. `$before_needle` (optional): If set to TRUE, `stristr()` returns the part of the haystack before the first occurrence of the needle. Default is FALSE.

Example:

<?php
$haystack = "Hello, world! This is a test string.";
$needle = "world";
// Case-insensitive search for "world" in $haystack
$result = stristr($haystack, $needle);
if ($result !== false) {
echo "Found: $result"; // Output: Found: world! This is a test string.
} else {
echo "Not found.";
}
?>

In this example, `stristr()` searches for the substring “world” in the haystack string. Since the search is case-insensitive, it finds “world” within the haystack, and the part of the haystack starting from “world” to the end is returned.