Thursday, July 30, 2026
HomeLanguagesPHP mb_substr_count() Function

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:

  • $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

RELATED ARTICLES

4 COMMENTS

Most Popular

Dominic
32520 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6903 POSTS0 COMMENTS
Nicole Veronica
12017 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12115 POSTS0 COMMENTS
Shaida Kate Naidoo
7023 POSTS0 COMMENTS
Ted Musemwa
7265 POSTS0 COMMENTS
Thapelo Manthata
6981 POSTS0 COMMENTS
Umr Jansen
6973 POSTS0 COMMENTS