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
<?php
echo mb_substr("我在中国", 0, 3);
// Output: 我在中
?>Note
Unlike the regular substr(), mb_substr() correctly handles multibyte characters such as Chinese.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
