PHP mb_stristr() Function
Last Updated :
13 Sep, 2023
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 ;
?>
|
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 );
?>
|
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
Share your thoughts in the comments
Please Login to comment...