PHP – mb_stristr() function


The mb_stristr() function in PHP is used to search the first existence of a string in another given string; this function is not case-sensitive. mb_stristr() searches for the first existence of the needle in a given haystack string and returns the portion of the haystack. It will return False if the needle is not found.

Syntax

string mb_stristr(
   $str_haystack,
   $str_needle,
   $bool_before_needle=false,
   $str_encoding=empty
)

Parameters

mb_stristr() accepts the following four parameters −

  • $str_haystack − This parameter is used to get the first existence of the string needle.

  • $str_needle − This parameter is used to find the string in a haystack or from the given string.

  • $bool_before_needle − This parameter is used to decide which portion of the haystack to return.

    • If it is set to True, then it returns all the haystack string from the starting to the first existence of the needle "excluding the needle".

    • If it is set to False, then it returns all the haystack string from the first existence of the needle to the end "including the needle".

  • $str_encoding − This parameter returns the position of the haystack or it will return False, if not found.

Return Values

mb_stristr() returns the portion of the haystack string if the needle is found. It returns False if the needle is not found.

Example 1

<?php
   //using string Hello World
   // using false needle
   $string=mb_stristr("Hello World","or", false);

   // output
   print_r($string);
?>

Output

orld

Example 2

<?php
   //using string Hello World
   // using true needle
   $string=mb_stristr("Hello World","or", true);

   // output
   print_r($string);
?>

Output

Hello W

Notemb_stristr() works just like strichr(), but the only difference is that mb_stristr() is case-insensitive.

Updated on: 11-Oct-2021

92 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements