mb_substr_count
Counts how many times a substring occurs in a string.
Syntax
mb_substr_count(string $haystack, string $needle, ?string $encoding = null): intParameters
| Parameter | Type | Description |
|---|---|---|
| $haystack | string | The string to search in. |
| $needle | string | What to look for. |
| $encoding | ?string | Optional. The encoding. |
Return value
intThe number of occurrences.
Examples
How many times a word occurs:
<?php
echo mb_substr_count('чай, кофе, чай, сок, чай', 'чай');Output:
3Notes
The search is case-sensitive. To ignore case, lower both strings with mb_strtolower().
See also
- substr_count — Counts how many times a substring occurs in a string.
- mb_strpos — Finds the position of the first occurrence of a substring.
- mb_strlen — Returns the number of characters in a string.
In the tutorial
- Strings — Working with strings in PHP — quotes, concatenation, length, searching, replacing and multibyte functions.