mb_substr_count

Count the number of substring occurrences

Description

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

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

Parameters

haystack

The string being checked.

needle

The string being found.

encoding
The encoding parameter is the character encoding. If it is omitted or null, the internal character encoding value will be used.

Return Values

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

Errors/Exceptions

As of PHP 8.0.0, a ValueError is thrown if encoding is an invalid encoding. Prior to PHP 8.0.0, an E_WARNING was emitted instead.

Changelog

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

Examples

Example #1 mb_substr_count example

<?php
echo mb_substr_count("This is a test", "is"); // prints out 2
?>

See Also

  • mb_strpos
  • mb_substr
  • substr_count