PHP 中的 substr_count() 函数

phpprogrammingserver side programming更新于 2025/9/25 12:52:17

substr_count() 函数用于计算子字符串的数量。

语法

substr_count(str,substr,begin,len)

参数

  • str − 需要检查的字符串

  • substr − 需要搜索的字符串

  • begin − 字符串中搜索的起始位置

  • len −搜索的长度

返回

substr_count() 函数返回子字符串在字符串中出现的次数。

示例

下面是一个例子 −

<?php
   $str = 'This is our example!'; echo strlen($str);
   echo "
"; echo substr_count($str, 'our'); ?>

输出

20
1

相关文章