Count the number of times "world" occurs in the string:
The substr_count() function counts the number of times a substring occurs in a string.
Note: The substring is case-sensitive.
Note: This function does not count overlapped substrings (see example 2).
Note: This function generates a warning if the start parameter plus the length parameter is greater than the string length (see example 3).
| Parameter | Description |
|---|---|
| string | Required. Specifies the string to check |
| substring | Required. Specifies the string to search for |
| start | Optional. Specifies where in string to start searching |
| length | Optional. Specifies the length of the search |
| Return Value: | Returns the the number of times the substring occurs in the string |
|---|---|
| PHP Version: | 4+ |
| Changelog: | The start and length parameters were added in PHP 5.1 |
Using all parameters:
Overlapped substrings:
If the start and length parameters exceeds the string length, this function will output a warning:
This will output a warning because the length value exceeds the string length (3+9 is greater than 12)
Complete PHP String Reference
The perfect solution for professionals who need to balance work, family, and career building.
More than 10 000 certificates already issued!
The HTML Certificate documents your knowledge of HTML.
The HTML5 Certificate documents your knowledge of advanced HTML5.
The CSS Certificate documents your knowledge of advanced CSS.
The JavaScript Certificate documents your knowledge of JavaScript and HTML DOM.
The jQuery Certificate documents your knowledge of jQuery.
The XML Certificate documents your knowledge of XML, XML DOM and XSLT.
The ASP Certificate documents your knowledge of ASP, SQL, and ADO.
The PHP Certificate documents your knowledge of PHP and SQL (MySQL).
Your message has been sent to W3Schools.