Backend Development 2 min read

PHP mb_substr_count: Counting Substring Occurrences

The article explains PHP's mb_substr_count function, detailing its syntax, parameters, return value, and provides a practical example showing how to count the number of times a substring appears within a multibyte string.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
PHP mb_substr_count: Counting Substring Occurrences

The mb_substr_count function in PHP counts how many times a given substring ( needle ) appears in a target string ( haystack ) while supporting multibyte encodings.

Syntax: int mb_substr_count(string $haystack, string $needle [, string $encoding = mb_internal_encoding()])

Description: It returns the number of occurrences of needle in haystack . If the optional encoding parameter is omitted, the internal character encoding is used.

Parameters:

haystack : The string to be examined.

needle : The substring to search for.

encoding (optional): The character encoding; defaults to the internal encoding.

Return value: An integer representing the count of needle occurrences in haystack .

Example:

<?php
echo mb_substr_count("This is a test", "is"); // Outputs 2
?>
BackendPHPString()substringphp-functionsmb_substr_count
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.