Home  >  Article  >  Backend Development  >  PHP function substr_count() that counts the number of times a substring appears in a string

PHP function substr_count() that counts the number of times a substring appears in a string

黄舟
黄舟Original
2017-11-06 13:37:442099browse

Example

Calculate the number of times "world" appears in string:

<?php
echo substr_count("Hello world. The world is nice","world");
?>

substr_count() function counts the number of times a substring appears in a string.

Note: Substrings are case-sensitive.

Note: This function does not count overlapping substrings (see Example 2).

Note: If the start parameter plus the length parameter is greater than the string length, this function generates a warning (see Example 3).

Syntax

substr_count(string,substring,start,length)
Parameters Description
string Required. Specifies the string to check.
substring Required. Specifies the string to be retrieved.
start Optional. Specifies where in the string to begin the search.
length Optional. Specifies the length of the search.

Technical details

In PHP 5.1,

更多实例

实例 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"
?>

实例 2

重叠的子串:

<?php
$str = "abcabcab"; 
echo substr_count($str,"abcab"); // This function does not count overlapped substrings
?>

实例 3

如果 start 和 length 参数超过字符串长度,该函数则输出一个警告:

<?php
echo $str = "This is nice";
substr_count($str,"is",3,9);
?>

由于长度值超过字符串的长度(3 + 9大于12)。所以这将输出一个警告。

举例:

<?php
$text = &#39;This is a test&#39;;
echo strlen($text) . &#39;<br />&#39;; // 输出14
 
echo substr_count($text, &#39;is&#39;) . &#39;<br />&#39;; // 2
 
// the string is reduced to &#39;s is a test&#39;, so it prints 1
echo substr_count($text, &#39;is&#39;, 3) . &#39;<br />&#39;;//实际上就是从第四个字符开始查找是否在$text中含有is
 
// the text is reduced to &#39;re &#39;, so it prints 0
echo substr_count($text, &#39;are&#39;, 16, 3) . &#39;<br />&#39;;
 
// the text is reduced to &#39;s i&#39;, so it prints 0echo substr_count($text, &#39;is&#39;, 3, 3);
 
// generates a warning because 5+10 > 14
echo substr_count($text, &#39;is&#39;, 5, 10) . &#39;<br />&#39;;
 
 
// prints only 1, because it doesn&#39;t count overlapped subtrings
$text2 = &#39;gcdgcdgcd&#39;;
echo substr_count($text2, &#39;gcdgcd&#39;) . &#39;<br />&#39;;
 ?>
Return value: Returns the occurrence of the substring in the string frequency.
PHP Version: 4+
##Update Log : newly addedstart and length parameters.

The above is the detailed content of PHP function substr_count() that counts the number of times a substring appears in a string. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn