Home >Backend Development >PHP Tutorial >How to get the number of occurrences of a substring with the substr_count function in PHP
The example in this article describes how to obtain the number of occurrences of a substring with the substr_count() function in PHP. Share it with everyone for your reference, the details are as follows:
substr_count() in PHP can be used to count the number of occurrences of a substring in a specified string.
substr_count() function is defined as follows:
substr_count(string,substring,start,length)
Parameter description:
string Required. Specifies the string to be checked.
substring required. Specifies the string to search for.
start Optional. Specifies where in the string to begin the search.
length Optional. Specifies the length of the search.
The sample code is as follows:
<?php $str="本站提供大量脚本代码及脚本特效下载"; echo substr_count($str,"脚本"); echo "<br/>"; echo substr_count($str,"脚本",16);//指定在第16个字符后开始搜索 echo "<br/>"; echo substr_count($str,"脚本",16,10);//指定从第16个字符开始往后搜索10个字符结束 ?>
The running results are as follows:
3 2 1
For more information about PHP string operations, please check out this site's special topic: "php string (string) usage summary"
Hope this article The above will be helpful to everyone in PHP programming.
The above introduces the method of using the substr_count function in PHP to obtain the number of occurrences of a substring, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.