The substr_count Function
The substr_count function counts
how many times a substring occurs in a string.
Works correctly only with Latin characters (single-byte characters).
Syntax
substr_count(string $haystack, string $needle, int $offset = 0, ?int $length = null): int
Example
Let's count how many times the substring
'test' appears in our string:
<?php
echo substr_count('test www test', 'test');
?>
Code execution result:
2
See Also
-
the
mb_substr_countfunction,
which works with multibyte encodings -
the
str_word_countfunction,
which counts the number of words in a string -
the
substr_countfunction,
which counts the number of substrings -
the
count_charsfunction,
which counts the number of characters