Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

mb_substr_count

Counts how many times a substring occurs in a string.

Syntax

mb_substr_count(string $haystack, string $needle, ?string $encoding = null): int

Parameters

ParameterTypeDescription
$haystackstringThe string to search in.
$needlestringWhat to look for.
$encoding?stringOptional. The encoding.

Return value

intThe number of occurrences.

Examples

How many times a word occurs:

<?php
 
echo mb_substr_count('чай, кофе, чай, сок, чай', 'чай');

Output:

3

Notes

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.

Thanks for reading!

Donate to the site — we work hard for you.