Saturday, December 6, 2025
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

1 COMMENT

Most Popular

Dominic
32427 POSTS0 COMMENTS
Milvus
103 POSTS0 COMMENTS
Nango Kala
6802 POSTS0 COMMENTS
Nicole Veronica
11944 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12012 POSTS0 COMMENTS
Shaida Kate Naidoo
6932 POSTS0 COMMENTS
Ted Musemwa
7185 POSTS0 COMMENTS
Thapelo Manthata
6881 POSTS0 COMMENTS
Umr Jansen
6867 POSTS0 COMMENTS