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.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
PHP mb_substr() Function: Multibyte‑Safe Substring Operation

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.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PHPStringSubstringmb_substrmultibyte
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

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.