PHP: substr_count() function
PHP: Count the number of substring occurrences
The substr_count() function is used to count the number of times a substring occurs in a string. Note that substring is case sensitive.
Version:
(PHP 4 and above)
Syntax:
substr_count(string1, string2, nstart, nlength)
Parameters:
Name | Description | Required / Optional |
Type |
---|---|---|---|
string1 | The string to search in. | Required | String |
string2 | The substring to search for. | Required | String |
nstart | The position where string to start searching. | Optional | Integer |
nlength | The length of the string to be searched for. | Optional | Integer |
Return value:
Returns a number.
Value Type: Integer.
Pictorial Presentation
Example:
<?php
$string1="Welcome to w3resource.com";
echo substr_count($string1,'co');
echo substr_count($string1,'co',4);
echo substr_count($string1,'co',4,4);
?>
Output:
2 1 0
View the example in the browser
See also
Previous: substr_compare
Next: substr_replace
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/php/function-reference/substr_count.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics