mb_substr_count()

mb_substr_count() — Count the number of substring occurrences.

Built-in function of PHP 8.5. The manual lists it as available in PHP 4 >= 4.3.0, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.

What it does

Counts the number of times the needle substring occurs in the haystack string.

Signature

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

Parameters

ParameterTypeWhat it takes
$haystackstringThe string being checked.
$needlestringThe string being found.
$encoding?stringThe encoding parameter is the character encoding. If it is omitted or null, the internal character encoding value will be used.

Return value

The number of times the needle substring occurs in the haystack string.

Example

A runnable snippet:

echo mb_substr_count("This is a test", "is"); // prints out 2

Changelog

VersionChange
8.0.0encoding is nullable now.
8.0.0Now throws a ValueError if encoding is an invalid encoding. Previously, an E_WARNING was emitted and false was returned.

See also

mb_strpos(), mb_substr(), substr_count()

All PHP 8.5 built-in functions

Reference card built from the PHP manual page mb_substr_count() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.