小白教程
所有教程
关于
Search
172.70.131.145
172.70.131.145
参数设置
贡献
退出
操作
编辑
移动
保护
信息
历史
删除
查看“Func string substr count”的源代码
本页内容
上一节:
Func_string_substr_compare
下一节:
Func_string_substr_replace
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
{{DISPLAYTITLE:substr_count()}}[[分类:PHP字符串函数参考手册|92]] = PHP substr_count() 函数 = [[:分类:PHP字符串函数参考手册]] 计算 "world" 在字符串中出现的次数: <sample title="" desc="" lang="php" hererun="1"> <?php echo substr_count("Hello world. The world is nice","world"); ?> </sample> substr_count() 函数计算子串在字符串中出现的次数。 '''注释:'''子串是区分大小写的。 '''注释:'''该函数不计数重叠的子串(参见实例 2) 。 '''注释:'''如果 start 参数加上 length 参数大于字符串长度,该函数则生成一个警告(参见实例 3)。 == 语法 == <pre> substr_count(string,substring,start,length) </pre> {| class="table table-striped table-hover" ! 参数 ! 描述 |- | string | 必需。规定要检查的字符串。 |- | substring | 必需。规定要检索的字符串。 |- | start | 可选。规定在字符串中何处开始搜索。 |- | length | 可选。规定搜索的长度。 |} == 技术细节 == {| class="table table-striped table-hover" | 返回值: | 返回子串在字符串中出现的次数。 |- | PHP 版本: | 4+ |- | 更新日志: | 在 PHP 5.1 中,新增了 start 和 length 参数。 |} == 更多实例 == == 实例 1 == 使用所有的参数: <sample title="" desc="" lang="php" hererun="1"> <?php $str = "This is nice"; echo strlen($str)."<br>"; // Using strlen() to return the string length echo substr_count($str,"is")."<br>"; // The number of times "is" occurs in the string echo substr_count($str,"is",2)."<br>"; // The string is now reduced to "is is PHP" echo substr_count($str,"is",3)."<br>"; // The string is now reduced to "s is PHP" echo substr_count($str,"is",3,3)."<br>"; // The string is now reduced to "s i" ?> </sample> == 实例 2 == 重叠的子串: <sample title="" desc="" lang="php" hererun="1"> <?php $str = "abcabcab"; echo substr_count($str,"abcab"); // This function does not count overlapped substrings ?> </sample> == 实例 3 == 如果 start 和 length 参数超过字符串长度,该函数则输出一个警告: <sample title="" desc="" lang="php" hererun="1"> <?php echo $str = "This is nice"; substr_count($str,"is",3,9); ?> </sample> 由于长度值超过字符串的长度(3 + 9大于12)。所以这将输出一个警告。 [[:分类:PHP字符串函数参考手册]]
返回至“
Func string substr count
”。
上一节:
Func_string_substr_compare
下一节:
Func_string_substr_replace