PHP String substr_count() Function
The substr_count() is a built-in function of PHP. It counts the number of times a substring occurs in the given string. The substr_count() is a case-sensitive function, which means it treats uppercase and lowercase alphabets differently.
For example – A substring “cha” is not present in the string “CHaru”.
Note: The substr_count() is a case-sensitive function.
This function provides the facility to calculate that how many number of times a substring has occurred in the main string? It also provides an option to search a substring in a given range of the index. If the start and length values, which are specified for search is greater than the passed string, then it returns a warning to the user. PHP 4 and above versions support this function.
Note: The substr_count() function does not count overlapped substrings. (See the example 3).
Syntax
The syntax of the substr_count() is given below, which accepts four parameters, two string, and two integer type values.
Parameters
$string (required): The $string parameter is the main string parameter in which occurrence’s of substring is counted. It is a mandatory parameter of substr_count() function.
$substring (required): The value passed in this parameter is searched in $string parameter and returns the counted occurrence of substring. It is also a mandatory parameter of substr_count() function.
$start (optional): This parameter consists of an integer value. It specifies that from where to start the counting. The $start is an optional parameter of this function. The value, which is passed in this parameter, is negative then it will start counting from the end of the string.
$length (optional): This parameter is an optional parameter and depends on $start parameter. The substr_count() will generate a warning, if $start and $length together ($start+$length) are greater than the length of $string. A negative length always counts from the end of the string.
Returns Value
The substr_count returns an integer means it returns the number of times a substring occurs in the main string.
Changelogs
- In PHP 5.1.0, two new parameters were added, i.e., $start and $length.
- From PHP 7.1.0, $start and $length supports negative value also. Length can be also 0 now.
Examples
Below some examples are given. With the help of these examples learn the practical implementation of substr_count() function in the program.
Example 1: Program without having optional parameters.
Output:
In the above example, “Good” substring is found 2 times in the main string.
2
Example 2: Program when $start parameter is passed.
Output:
In the above example, “Good” substring is found only once in the main string, as searching is started from 5th position.
1
Example 3: Program when $start and $length both parameters are passed.
Output:
In the above example, “Good” substring is not found in the main string, because searching is started from 5th position to 15th ($start+$length).
0
Example 4: Case-sensitivity
Output:
In the above example, “LIFE” substring is not found in the main string, because substr_compare() is a case-sensitive function.
0
Example 5:
Output:
A warning is occurred here, because start + length > strin, i.e., (5+12 > 15). The length of $start + $length parameter should not be exceeded the length of the main string.
Warning: substr_count(): Invalid length value in C:xampphtdocsxamppPMAsubstr_count.php on line 4
Example 6: Overlapped Substring
Output:
It returned 1, as it didn’t count the overlapped substring.
1