Thursday, August 28, 2025
HomeLanguagesPHP mb_stristr() Function

PHP mb_stristr() Function

The mb_stristr() is an inbuilt function in PHP that is used to get the first occurrence of a string within another string. It will check case insensitivity.

Syntax:

mb_stristr(
    $haystack, 
    $needle, 
    $before_needle, 
    $encoding = null
): string|false

Parameters:

This function accepts 4 parameters that are described below:

  • $haystack: This is the string parameter where we search for our first occurrence of the string.
  • $needle: This is the substring that we want to search in the $haystack parameter.
  • $before_needle: This is the optional parameter that determines whether to return haystack after or before $needle occurs. If this parameter is “true” it will return beginning to the first occurrence of the $needle string (excluding needle). If this parameter is set to “false”, It will return from the beginning of the $needle to the $needle end.
  • $encoding: This is the optional parameter that specifies the character encoding in the $haystack and $needle parameters. If the encoding is not provided, it will use character encoding used.

Return Value:

This function returns the portion of $haystack if the needle is found in a $haystack, otherwise, it will return “false”.

Program 1: The following program demonstrates the mb_stristr() Function.

PHP




<?php
$string = "Hello World";
$sub = "WORLD";
$result = mb_stristr($string, $sub);
echo $result;
?>


Output

World

Program 2: The following program demonstrates the mb_stristr() Function.

PHP




<?php
$string = "I love PHP";
$sub = "PYTHON";
$result = mb_stristr($string, $sub);
var_dump($result);
?>


Output

bool(false)

Program 3: The following program demonstrates the mb_stristr() Function.

PHP




<?php
$string = "Hello World";
$sub = "WORLD";
$result = mb_stristr($string, $sub);
  
if ($result !== false) {
    echo "Substring '$sub' found in '$string'";
} else {
    echo "Substring '$sub' not found in '$string'";
}
?>


Output

Substring 'WORLD' found in 'Hello World'

Reference: https://www.php.net/manual/en/function.mb-stristr.php                                                                                                                              

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32244 POSTS0 COMMENTS
Milvus
80 POSTS0 COMMENTS
Nango Kala
6615 POSTS0 COMMENTS
Nicole Veronica
11787 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11832 POSTS0 COMMENTS
Shaida Kate Naidoo
6728 POSTS0 COMMENTS
Ted Musemwa
7009 POSTS0 COMMENTS
Thapelo Manthata
6684 POSTS0 COMMENTS
Umr Jansen
6697 POSTS0 COMMENTS