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.
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
?>Laravel Tech Community
Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.