函数名:substr_count()
适用版本:PHP 4 >= 4.0.5, PHP 5, PHP 7
函数描述:substr_count() 函数用于计算字符串中一个子字符串出现的次数。
语法:int substr_count ( string $haystack , string $needle [, int $offset = 0 [, int $length ]] )
参数:
返回值:返回子字符串在字符串中出现的次数,如果没有出现则返回 0。
示例 1:
$str = 'Hello, World!';
$count = substr_count($str, 'o');
echo $count; // 输出 2
示例 2:
$str = 'abcdeabcdeabcde';
$count = substr_count($str, 'abc', 2, 6);
echo $count; // 输出 2
上述示例中,substr_count() 函数在示例 1 中计算了字符串 "Hello, World!" 中字母 "o" 的出现次数,结果为 2。在示例 2 中,它计算了字符串 "abcdeabcdeabcde" 中从索引位置 2 开始的 6 个字符中子字符串 "abc" 的出现次数,结果同样为 2。