Thursday, November 13, 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

Most Popular

Dominic
32399 POSTS0 COMMENTS
Milvus
95 POSTS0 COMMENTS
Nango Kala
6765 POSTS0 COMMENTS
Nicole Veronica
11916 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11984 POSTS0 COMMENTS
Shaida Kate Naidoo
6889 POSTS0 COMMENTS
Ted Musemwa
7141 POSTS0 COMMENTS
Thapelo Manthata
6836 POSTS0 COMMENTS
Umr Jansen
6839 POSTS0 COMMENTS