PHP mb_substr() Function: Multibyte‑Safe Substring Operation
This article explains PHP’s mb_substr() function, detailing its multibyte‑safe substring behavior, parameter definitions, return value, and provides a practical code example illustrating how to extract a portion of a Chinese string correctly.
mb_substr() performs a multibyte‑safe substring operation on a string, counting positions from the start (0‑based) and optionally specifying length and encoding.
Parameters
string $str : the input string.
int $start : the starting character position.
int $length = NULL : maximum number of characters to extract; if omitted or NULL, extracts to the end.
string $encoding : character encoding; defaults to the internal encoding.
Return value
The function returns the portion of $str defined by $start and $length .
Example
Note
Unlike the regular substr() , mb_substr() correctly handles multibyte characters such as Chinese.
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.