Open In App

PHP mb_substr_count() Function

The  mb_substr_count() function is an inbuilt function in PHP that counts the occurrence of strings within a given string.

Syntax:



mb_substr_count($haystack, $needle, $encoding): int

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

Return Value: The number of times $needle string occurs in the $haystack string will be returned by this function.



Example 1: The following code demonstrates the mb_substr_count() function.




<?php
  
$string = "Geeks for Geeks";
$search_string = "for";
  
echo mb_substr_count($string, $search_string);
  
?>

Output:

1

Example 2: The following code demonstrates the mb_substr_count() function.




<?php
$string = "Geeks for Geeks is a learning platform "
             . "where we can learn any programming language";
$search_string = "a";
  
echo mb_substr_count($string, $search_string);
?>

Output:

9

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

Article Tags :