Easy Tutorial
❮ Func String Soundex Func Array Sizeof ❯

PHP substr_count() Function

PHP String Reference Manual

Example

Count the number of times "world" appears in the string:


The substr_count() function counts the number of occurrences of a substring within 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 exceeds the string length, the function generates a warning (see Example 3).


Syntax

Parameter Description
string Required. Specifies the string to check.
substring Required. Specifies the string to search for.
start Optional. Specifies where to start in the string.
length Optional. Specifies the length of the search.

Technical Details

Return Value: Returns the number of occurrences of the substring in the string.
PHP Version: 4+
--- ---
Changelog: Added the start and length parameters in PHP 5.1.
--- ---

More Examples

Example 1

Using all parameters:

Example 2

Overlapping substrings:

Example 3

If the start and length parameters exceed the string length, the function outputs a warning:

Since the length value exceeds the string length (3 + 9 is greater than 12). This will output a warning.


❮ Func String Soundex Func Array Sizeof ❯