PHP mb_substr Function: Multibyte‑Safe Substring Extraction

This article explains the PHP mb_substr() function, detailing its multibyte‑safe substring behavior, parameter list (str, start, length, encoding), return value, and provides a clear example showing how to extract the first two characters from a Chinese string.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
PHP mb_substr Function: Multibyte‑Safe Substring Extraction

The PHP mb_substr() function retrieves a portion of a string in a multibyte‑safe manner.

It counts positions from the start of the string, with the first character at index 0, the second at 1, and so on.

Signature:

string mb_substr(string $str, int $start[, int $length = NULL[, string $encoding = mb_internal_encoding()]])

Parameters:

str : the source string.

start : the position of the first character to include.

length : the maximum number of characters to extract; if omitted or NULL, extracts to the end of the string.

encoding : character encoding; defaults to the internal encoding if omitted.

Return value: the extracted substring according to the given start and length.

Example:

<?php
echo mb_substr("帅哥美女", 0, 2);
// Output: 帅哥
?>
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.

BackendPHPStringfunctionmb_substr
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.