Open In App

PHP mb_strstr() Function

The mb_strstr() function is an inbuilt function in PHP that finds the first occurrence of a given string in the main string, i.e. it will search for the occurrence of the first needle in haystack, & id found then the portion of the haystack will be returned, otherwise return false.

Syntax:



mb_strstr(
    string $haystack,
    string $needle,
    bool $before_needle = false,
    ?string $encoding = null
): string|false

Parameters: This function accepts four parameters that are described below:

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



Example 1: The following program demonstrates mb_strstr() function.




<?php
  
$string = "Hello, world!";
$sub = "world";
  
$pos = mb_strstr($string, $sub);
  
echo $pos;
?>

Output:

world!     

Example 2: The following program demonstrates mb_strstr() function




<?php
  
$string = "Geeks for Geeks";
$sub = "for";
  
$pos = mb_strstr($string, $sub, true);
  
echo $pos;
  
?>

Output:

Geeks 

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


Article Tags :