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.
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/>';
?>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.
