Using mb_strcut() to Extract Substrings by Bytes in PHP

This article explains how the PHP mb_strcut() function extracts a substring based on byte positions, compares it with mb_substr() and substr(), details its parameters and return value, and provides a complete example demonstrating byte‑level string cutting for multibyte text.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
Using mb_strcut() to Extract Substrings by Bytes in PHP

mb_strcut() – Getting a Portion of a String by Bytes

The mb_strcut() function extracts a substring from a string based on byte positions, similar to mb_substr() but operates on bytes rather than characters, preventing malformed byte sequences that substr() may produce.

Parameters

str : The input string to be cut.

start : Starting position in bytes.

length : Byte length to extract; if omitted or NULL, extracts to the end.

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

Return

The function returns the portion of str defined by start and length.

Example

<?php
header("content-Type:text/html;charset=utf-8");
// test
$cn_str = "钓鱼岛是中国的hehe";
$en_str = "this is just a test";

echo "mb_strcut-3:" . mb_strcut($cn_str, 0, 3) . '<br/><br/>';
// byte-based cut
echo "mb_strcut-4:" . mb_strcut($cn_str, 0, 4) . '<br/><br/>';
?>
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.

Backendstring-manipulationmb_strcut
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.