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