mb_str_split()
mb_str_split() — Given a multibyte string, return an array of its characters.
Built-in function of PHP 8.5. The manual lists it as available in PHP 7 >= 7.4.0, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.
What it does
This function will return an array of strings, it is a version of str_split() with support for encodings of variable character size as well as fixed-size encodings of 1,2 or 4 byte characters. If the length parameter is specified, the string is broken down into chunks of the specified length in characters (not bytes). The encoding parameter can be optionally specified and it is good practice to do so.
Signature
mb_str_split(string $string, int $length = 1, ?string $encoding = null): array
Parameters
| Parameter | Type | What it takes |
|---|---|---|
| $string | string | The string to split into characters or chunks. |
| $length | int | If specified, each element of the returned array will be composed of multiple characters instead of a single character. |
| $encoding | ?string | A string specifying one of the supported encodings. |
Return value
mb_str_split() returns an array of strings.
Example
The PHP manual ships no example for mb_str_split(); the signature and the parameter table above are the contract.
Changelog
| Version | Change |
|---|---|
| 8.0.0 | encoding is nullable now. |
| 8.0.0 | Now throws a ValueError if encoding is an invalid encoding. Previously, an E_WARNING was emitted and false was returned. |
| 8.0.0 | This function no longer returns false on failure. |
See also
All PHP 8.5 built-in functions
Reference card built from the PHP manual page mb_str_split() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.