PHP – How to count the number of substring using mb_substr_count()?


In PHP, we can use the function mb_substr_count() to count the total number of substrings in a given string.

Syntax

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

Parameters

mb_substr_count() accepts three parameters: $haystack, $needle and $encoding.

  • $haystack− This parameter will check the string.

  • $needle− This parameter will be used to tell that the substring is found from the given total string.

  • $encoding− This parameter is the character encoding. If it is absent or null, then the internal character encoding value will be used.

Return Values

This will return the number of times the needle substring occurs in the haystack string.

Example

 Live Demo

<?php
   // mb_substr_count function is used
   $string= mb_substr_count("This is a test", "is");
   echo "$string";
?>

Output

2

Updated on: 23-Aug-2021

133 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements