Current Location: Home> Function Categories> mb_substr_count

mb_substr_count

Calculate the number of occurrences of substrings
Name:mb_substr_count
Category:Multi-byte string
Programming Language:php
One-line Description:Calculate the number of times a string appears in another string, case-insensitive

Function name: mb_substr_count()

Function description: The mb_substr_count() function is used to calculate the number of times a string appears in another string, and is case-insensitive.

Function usage: mb_substr_count(string $haystack, string $needle, string|null $encoding = null): int

parameter:

  • $haystack: Required, string to search.
  • $needle: Required, substring to search for.
  • $encoding: optional, specify character encoding. Default is internal character encoding.

Return value: Returns the number of times a substring appears in the string.

Example:

 $text = "Hello, I'm a PHP developer."; $substring = "php"; $count = mb_substr_count(mb_strtolower($text), mb_strtolower($substring)); echo "子字符串出现的次数为: " . $count;

Output: The number of times the substring appears is: 1

Description: In the above example, we used the mb_substr_count() function to calculate the number of times the substring "php" appears in the string "Hello, I'm a PHP developer." Since the function is case-insensitive, we used the mb_strtolower() function to convert the string to lowercase before the calculation. Finally, we output the calculation result as "The number of times the substring appears is: 1".

Similar Functions
Popular Articles