- 1
- 3
- 2
- 4
Instance
Calculate the number of occurrences of "Shanghai" in a string:
<?phpecho substr_count ("I Love Shanghai". Shanghai is the "biggest city", "Shanghai");? >
Running an instance
The Substr_count () function calculates the number of occurrences of a substring 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 size, the function generates a warning (see Example 3).
Grammar
Substr_count (string,substring,start,length)
Parameters |
Description |
String |
Necessary. Specifies the string to be checked. |
Substring |
Necessary. Specifies the string to search for. |
Start |
Optional. Specifies where to start the search in the string. |
Length |
Optional. Specifies the length of the search. |
Technical details
return value: |
Returns the number of occurrences of a substring in a string. |
PHP version: |
4 + |
Update log: |
In PHP 5.1, the start and length parameters have been added. |
More examples of examples 1
Use all the parameters:
String reduction to "s I"?>
Running an instance
Example 2
Overlapping sub-strings:
This function does not count overlapping substrings?>
Running an instance
Example 3
If the start and length parameters exceed the string length, the function outputs a warning:
<?phpecho $str = "This was nice"; Substr_count ($str, ' is ', 3,9);? >
Because the length value exceeds the length of the string (3 + 9 is greater than 12), the use outputs a warning.
Correct answer:
[Single topic] The results of the following programs are: <?php $STR = "Tomorrow Programming Dictionary"; Echo Substr_count ($str, "word");? >