: This article describes how to use the substr_count function in PHP to obtain the number of occurrences of a substring. For more information about PHP tutorials, see. This example describes how to obtain the occurrence times of a substring using the substr_count () function in PHP. We will share this with you for your reference. The details are as follows:
Substr_count () in PHP can be used to calculate the number of times a specified string substring appears.
The substr_count () function is defined as follows:
Substr_count (string, substring, start, length)
Parameter description:
String is required. Specifies the string to be checked.
Substring is required. Specifies the string to be searched.
Start is optional. Specifies where to start searching in the string.
Length is optional. Specify the length of the search.
The sample code is as follows:
<? Php $ str = "this site provides massive download of script code and special script effects"; echo substr_count ($ str, "script"); echo"
"; Echo substr_count ($ str," script ", 16); // specify to search for echo after 16th characters"
"; Echo substr_count ($ str," script ", 16th); // specifies that the search will end with 10 characters starting from characters.
The running result is as follows:
321