PHP substr_count()

王林
发布: 2024-08-29 12:51:09
原创
919 人浏览过

PHP 编程语言的 substr_count() 函数也是重要的内置函数之一,它对于计算特定给定字符串中出现了多少个子字符串非常有帮助。该函数将为给定索引范围内的某些给定子字符串提供搜索选项。 substr_count() 区分大小写,这意味着字符串“acbcab”中不存在“abc”子字符串值。如果为搜索指定的 (start+length) 值超出了传递的字符串的大小,则会向用户返回警告消息。

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

PHP 中 substr_count() 函数的语法

语法如下:

Substr_count($string1, $substring1, $start1, $length1)
登录后复制

substr_count() 函数的参数

substr_count()函数参数说明:

1。 $string1: PHP 编程语言的 substr_count() 的 string1 参数有助于传递参数,该参数实际上是计算子字符串出现次数的参数。 string1 参数是 substr_count() 函数的强制参数之一。需要它才能使 substr_count() 正常工作。

2。 $substring1: PHP 编程语言的 substr_count() 的 substring1 参数有助于传递子字符串,其中字符串搜索和出现次数将被统计并返回。必须提供此 substring1 参数,因为它是强制参数。

3。 $start1: PHP 编程语言的 substr_count() 的 start1 参数有助于将某些值传递给该参数,但它不是 PHP substr_count() 函数的强制参数。它是一个可选参数。参数传递值后,搜索将完成,并从起始位置开始,而不是整个字符串搜索不同出现的子字符串。

4。 $length1: PHP 编程语言的 substr_count() 的 length1 参数将有助于限制实际从 start 开始到 start+length 位置的搜索操作。如果 start+length 值将增加字符串长度,则通过提供/生成警告消息。该参数根本不是强制参数。这是一个可选参数。

substr_count() 函数在 PHP 中如何工作?

substr_count() 函数通过返回不同类型的值来工作,这些值在不同类型的情况下如下所示。

  • 仅当字符串具有一些根本未传递的可选参数时,给定的子字符串才会出现/出现多次。
  • 当字符串值作为参数传递时,子字符串会在字符串起始点到字符串结束点之间出现多次。
  • 当同时传递 length 和 start 参数时,子字符串将出现在字符串内部/字符串中,位于字符串的开头和字符串位置的 start+length 之间。
  • substr_count() 函数基本上通过计算特定大字符串/其他字符串内的特定子字符串或子字符串的出现次数来工作。 substr_count() 将提供在某个索引范围内搜索特定子字符串的选项。

PHP substr_count() 的实现示例

以下是 substr_count() 函数的示例:

示例#1

这是仅使用两个强制参数实现 substr_count() 的示例。它们是原始字符串和子字符串。将在 substr_count() 内部的原始源字符串中搜索子字符串,以了解子字符串出现了多少次。

代码:

<?php
$str1 = "pavan sake pavan";
echo "<hr>";
echo "The Original Source String is :: $str1";
echo "<br>";
$substring1 = "pavan";
echo "The Sub String which is to be searched in the original source string :: $substring1";
echo "<br>";
echo "The number of occurrences of the substring ($substring1) is :: ";
echo substr_count($str1, $substring1);
echo "<hr>";
?>
登录后复制

输出:

PHP substr_count()

示例#2

这是传入start参数时实现substr_count()函数的示例。

代码:

<?php
echo "This is the example of implementing start1 variable/parameter inside of substring_count() function";
echo "<br>";
$str1 = "pavankumar sake pavan";
echo "<hr>";
echo "The Original Source String is :: $str1";
echo "<br>";
$substring1 = "pavan";
echo "The Sub String which is to be searched in the original source string :: $substring1";
echo "<br>";
echo "The number of occurrences of the substring ($substring1) after the string length 6 is :: ";
echo substr_count($str1, $substring1, 6);
echo "<hr>";
?>
登录后复制

输出:

PHP substr_count()

示例#3

这是在同时传递 start1 和 length1 参数时实现 substr_count() 的示例。这将绕过搜索 2 次。

代码:

<?php
echo "This is the example of implementing length1 and start1 variables/parametersinside of substring_count() function :: ";
echo "<br>";
$str1 = "pavankumar sake pavan sake sakesakesakesake";
echo "<hr>";
echo "The Original Source String of str1 variable is :: $str1";
echo "<br>";
$substring1 = "pavan";
echo "The Sub String substring1 variable which is to be searched in the original source string :: $substring1";
echo "<br>";
echo "The number of occurrences of the substring1 ($substring1) after the start value 6 and length value 2 is :: ";
echo substr_count($str1, $substring1, 6, 2);
echo "<hr>";
?>
登录后复制

输出:

PHP substr_count()

Example #4

This is the example of implementing the substr_count() function when the string length exceeds the start1+length1 parameters value. This will produce a warning type of message. Check out the output so that you will understand the substr_count() function.

Code:

<?php
echo "This is the example of implementing length1 and start1 variables/parameters inside of substring_count() function for warning message if length exceeds the string length :: ";
echo "<br>";
$str1 = "pavankumar sake pavan sake sakesakesakesake";
echo "<hr>";
echo "The Original Source String of str1 variable is :: $str1";
echo "<br>";
$substring1 = "pavan";
echo "The Sub String substring1 variable which is to be searched in the original source string :: $substring1";
echo "<br>";
echo "<hr>";
echo "The number of occurrences of the substring1 ($substring1) after the start value 6 and length value 2 is :: ";
echo "<hr>";
echo substr_count($str1, $substring1, 6, 121);
echo "<hr>";
?>
登录后复制

Output:

PHP substr_count()

Conclusion

I hope you learned what is the definition of the PHP substr_count() function along with substr_count()’s syntax and parameter, how the PHP substr_count() function works in the PHP Programming language along with various examples to understand the substr_count() concept better and so easily.

Recommended Article

This is a guide to the PHP substr_count(). Here we discuss the Introduction and its parameters of PHP substr_count() Function along with examples and Code Implementation. You can also go through our other suggested articles to learn more-

  1. PHP ob_start()
  2. Abstract Class in PHP
  3. PHP chop()

以上是PHP substr_count()的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
php
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!