Home >Backend Development >PHP Tutorial >PHP's substr_count() function: How to count the number of occurrences of a substring in a string

PHP's substr_count() function: How to count the number of occurrences of a substring in a string

王林
王林Original
2023-11-03 10:13:071248browse

PHPs substr_count() function: How to count the number of occurrences of a substring in a string

PHP's substr_count() function: How to count the number of occurrences of a substring in a string requires specific code examples

In PHP, the substr_count() function is Used to count the occurrences of a string within another string. The substr_count() function is very useful, especially when you need to count the number of times a specific substring appears in text.

The syntax of the substr_count() function is as follows:
int substr_count ( string $string , string $substring [, int $start = 0 [, int $length ]] )

$str : The string to be checked;
$sub: The substring to be checked;
$start: The position to start checking, the default is 0;
$length: The length to be checked, the default is the entire character string.

Next, we will use a specific example to show how to use the substr_count() function to count the number of occurrences of a substring in a string.

<?php

// 在一个字符串中计算子字符串的出现次数
$string = "Hello world, Hello PHP, Hello substr_count!";
$sub = "Hello";
$count = substr_count($string, $sub);

// 输出结果
echo "字符串中子字符串 "$sub" 出现的次数为:$count";

?>

In the above code, we assign the string to be checked to $string, and the substring to be checked to $sub. The substr_count() function is used to count the number of occurrences of a substring in a string and assigns the result to the variable $count.

Finally, use the echo statement to output the number of occurrences of the substring. In our example, the number of occurrences of the substring "Hello" in the string is 3.

In addition, the substr_count() function can also be used in some more complex applications. For example, in a text editor, you may need to count the number of times a certain keyword appears in a piece of text entered by the user. You can accomplish this task easily using the substr_count() function.

In short, the substr_count() function is an important function in PHP for counting the number of occurrences of a substring in a string. By calling the substr_count() function passing the original string and substring as arguments, we can concisely count the number of occurrences of the substring in the original string. With this function, we can more easily get the results we want when dealing with some statistical work involving strings.

The above is the detailed content of PHP's substr_count() function: How to count the number of occurrences of a substring 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