PHP 编程语言的 substr_count() 函数也是重要的内置函数之一,它对于计算特定给定字符串中出现了多少个子字符串非常有帮助。该函数将为给定索引范围内的某些给定子字符串提供搜索选项。 substr_count() 区分大小写,这意味着字符串“acbcab”中不存在“abc”子字符串值。如果为搜索指定的 (start+length) 值超出了传递的字符串的大小,则会向用户返回警告消息。
开始您的免费软件开发课程
网络开发、编程语言、软件测试及其他
语法如下:
Substr_count($string1, $substring1, $start1, $length1)
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() 函数通过返回不同类型的值来工作,这些值在不同类型的情况下如下所示。
以下是 substr_count() 函数的示例:
这是仅使用两个强制参数实现 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>"; ?>
输出:
这是传入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>"; ?>
输出:
这是在同时传递 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>"; ?>
输出:
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:
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.
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-
以上是PHP substr_count()的详细内容。更多信息请关注PHP中文网其他相关文章!