当前位置: 首页> 函数类别大全> mb_substr_count

mb_substr_count

计算子字符串出现次数
名称:mb_substr_count
分类:多字节字符串
所属语言:php
一句话介绍: 计算一个字符串在另一个字符串中出现的次数,不区分大小写

函数名称:mb_substr_count()

函数描述:mb_substr_count() 函数用于计算一个字符串在另一个字符串中出现的次数,不区分大小写。

函数用法: mb_substr_count(string $haystack, string $needle, string|null $encoding = null): int

参数:

  • $haystack:必需,要搜索的字符串。
  • $needle:必需,要搜索的子字符串。
  • $encoding:可选,指定字符编码。默认为内部字符编码。

返回值: 返回子字符串在字符串中出现的次数。

示例:

$text = "Hello, I'm a PHP developer.";
$substring = "php";

$count = mb_substr_count(mb_strtolower($text), mb_strtolower($substring));
echo "子字符串出现的次数为: " . $count;

输出: 子字符串出现的次数为: 1

说明: 上述示例中,我们使用了 mb_substr_count() 函数来计算子字符串 "php" 在字符串 "Hello, I'm a PHP developer." 中出现的次数。由于该函数不区分大小写,我们在计算之前使用了 mb_strtolower() 函数将字符串转换为小写。最后,我们将计算结果输出为 "子字符串出现的次数为:1"。

同类函数
热门文章