When working with PHP, you may need to split a string into Unicode-character chunks. This guide explains the operation, shows what its arguments mean, and uses three separate examples to make the result easier to understand.
mb_str_split: Chunks are based on multibyte characters, not grapheme clusters such as base letters with combining marks.
What Is mb_str_split in PHP?
Split a string into Unicode-character chunks. Chunks are based on multibyte characters, not grapheme clusters such as base letters with combining marks.
Syntax:
mb_str_split(string $string, int $length = 1, ?string $encoding = null): array
Parameters and return value: The signature above identifies the arguments and return type where the API has a single call. For topic guides, follow the sequence of calls in the examples. Always distinguish a returned value from an object or variable modified by reference.
Important: The chunk length must be greater than zero. Emoji sequences can still be split between code points.
Example 1: Basic Usage
This example introduces the operation with a short and predictable input.
<?php
print_r(mb_str_split("Café", 1, "UTF-8"));
The code focuses on the main operation. Chunks are based on multibyte characters, not grapheme clusters such as base letters with combining marks.
Example 2: Another Case
Now compare a different input, locale, date, or option to see another part of the behavior.
<?php
print_r(mb_str_split("ABCDEFG", 3, "UTF-8"));
What changes? The chunk length must be greater than zero. Emoji sequences can still be split between code points.
Practical Example: Applying It in an Application
Use the following separate example as a starting point for a small application task. Check any extension or environment requirements below before running it.
<?php
$code = "A1B2C3";
$parts = mb_str_split($code, 2, "UTF-8");
foreach ($parts as $part) {
echo $part . PHP_EOL;
}
The practical example demonstrates how to apply this operation to multiple values, a reusable task, or a validated input. Chunks are based on multibyte characters, not grapheme clusters such as base letters with combining marks.
Important Behavior and Edge Cases
- Core behavior: Chunks are based on multibyte characters, not grapheme clusters such as base letters with combining marks.
- Inputs and results: The chunk length must be greater than zero. Emoji sequences can still be split between code points.
- Alternatives: grapheme_str_split() is designed for user-perceived grapheme clusters.
- Compatibility: mbstring is required for mb_* functions.
Comparison With Related Functions
| Function or Approach | When to Use It |
|---|---|
mb_str_split | Split a string into unicode-character chunks. |
| Related PHP approach | grapheme_str_split() is designed for user-perceived grapheme clusters. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Using an incorrect input or option | The chunk length must be greater than zero. Emoji sequences can still be split between code points. |
| Confusing this API with a related operation | grapheme_str_split() is designed for user-perceived grapheme clusters. |
| Assuming output is identical across environments | Check return values and required extensions or versions. mbstring is required for mb_* functions. |
Key Takeaways
- mb_str_split helps you split a string into Unicode-character chunks.
- Chunks are based on multibyte characters, not grapheme clusters such as base letters with combining marks.
- The chunk length must be greater than zero. Emoji sequences can still be split between code points.
Use mb_str_split when you need to split a string into Unicode-character chunks. Start with the first example, compare the second, and adapt the practical example to your inputs. Confirm the return value and environment requirements before adding it to production code.