Open In App

PHP mb_substr_count() Function

Last Updated : 28 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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:

  • $haystack:  This is the main parameter where we check if our $needle parameter string is in this string parameter or not.
  • $needle: This is the substring that you want to search for the input string. It can be of any length.
  • $encoding: This is the optional parameter that defines which type of encoding you are using in the input.

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




<?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




<?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


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads