Definition and Usage
- PHP Version
- 7.4+
The mb_str_split() function splits a multibyte string into substrings of a specified length and returns an array.
The return value is an array containing the split substrings.
It provides almost the same functionality as the str_split() function, but the key difference is that it safely handles multibyte strings, including Japanese, Korean, and Chinese characters.
Additional Explanation
In UTF-8 encoding, ASCII characters such as English letters and numbers are represented using 1 byte, while characters such as Japanese, Korean, and Chinese characters require multiple bytes.
Characters that use 2 or more bytes to represent a single character are called multibyte characters.
This function was introduced in PHP 7.4 and is not supported in earlier versions.
Basic Example
$str = 'こんにちは!';
/* Example of splitting a string into substrings with a length of 1
Difference between str_split() and mb_str_split() */
// Does not support multibyte characters (length is measured in bytes)
$arr1 = str_split($str, 1);
print_r($arr1);
/* Output:
Array
(
[0] => �
[1] => �
[2] => �
[3] => �
[4] => �
[5] => �
[6] => �
[7] => �
[8] => �
[9] => �
[10] => �
[11] => �
[12] => �
[13] => �
[14] => �
[15] => !
)
*/
// Safely handles multibyte strings (length is measured in characters)
$arr2 = mb_str_split($str, 1, 'UTF-8');
print_r($arr2);
/* Output:
Array
(
[0] => こ
[1] => ん
[2] => に
[3] => ち
[4] => は
[5] => !
)
*/
Syntax
mb_str_split(string $string, int $length = 1, ?string $encoding = null): array
Parameters
$string |
Required. The string to split. |
|---|---|
$length |
Optional. Specifies the length of each substring. If this parameter is omitted, the default value 1 is used, and an array is created where each character is an element. |
$encoding |
Optional. Specifies the encoding of the string.
The default value is null. If this parameter is omitted or null is passed, the internal character encoding is used.
|
Return Values
Splits the string into substrings of the length specified by the $length parameter and returns them as an array.
The length is measured in characters.
Changelog
| Version | Description |
|---|---|
| 8.0.0 | Starting from PHP 8.0.0, the $encoding parameter has been changed to nullable. This means that the $encoding parameter can now be omitted or set to null. Previously, a valid encoding had to be specified. |
| 8.0.0 | Starting from PHP 8.0.0, the function no longer returns false when it fails. Previously, the function returned false on failure, but it now always returns an array. If the function fails, an empty array is returned. |
Practical Examples
The mb_str_split() function is useful in various practical scenarios because it easily splits both single-byte and multibyte strings.
Let's look at a few examples.
Reverse a String
To reverse a string, you can use the mb_str_split() function to split the string into an array, and then use the array_reverse() function to reverse the order of the array.
Then, convert the reversed array back into a string using the implode() function.
$str = 'こんにちは';
$array = mb_str_split($str, 1, 'UTF-8');
$reversed_array = array_reverse($array);
$reversed_string = implode('', $reversed_array);
echo $reversed_string; // Output: 'はちにんこ'
Additional Explanation
The array_reverse() function returns a new array with the elements of a given array in reverse order.
Additional Explanation
The implode() function joins array elements into a string using a custom delimiter.
Hiding Part of Names or Usernames
To hide sensitive information, such as a name or username, you can use the mb_str_split() function to split the string into an array and modify specific characters.
For example, when masking part of a username with *, this function safely converts a multibyte string into an array without breaking characters.
function mask_username($username) {
// Split a multibyte string into individual characters
$chars = mb_str_split($username, 1, 'UTF-8');
$length = count($chars);
if ($length <= 2) {
return $username;
}
// Replace characters except the first and last characters
for ($i = 1; $i < $length - 1; $i++) {
$chars[$i] = '*';
}
return implode('', $chars);
}
echo mask_username("johndoe"); // j*****e
echo "<br>";
echo mask_username("Michael"); // M*****l
echo "<br>";
echo mask_username("こんにちは"); // こ***は
echo "<br>";
echo mask_username("山田太郎"); // 山**郎
Additional Explanation
The for loop is a control structure used to repeat a block of code a specific number of times or within a specified range.
The examples above demonstrate how the mb_str_split() function can be used to easily handle strings.
This function is useful for splitting strings into substrings, regardless of whether they contain multibyte characters, allowing them to be processed in various ways.