Home > Backend Development > PHP Tutorial > How to get the number of occurrences of a substring with the substr_count function in PHP

How to get the number of occurrences of a substring with the substr_count function in PHP

WBOY
Release: 2016-07-29 09:07:28
Original
1120 people have browsed it

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:

<&#63;php
 $str="本站提供大量脚本代码及脚本特效下载";
 echo substr_count($str,"脚本");
 echo "<br/>";
 echo substr_count($str,"脚本",16);//指定在第16个字符后开始搜索
 echo "<br/>";
 echo substr_count($str,"脚本",16,10);//指定从第16个字符开始往后搜索10个字符结束
?>

Copy after login

The running results are as follows:

3
2
1

Copy after login

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.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template