How to use mb_str_split() Function In PHP
The mb_str_split() function is used to convert a multibyte string to an array of characters. This function is useful for handling multibyte encodings such as UTF-8.
Syntax:
array mb_str_split($string, $length, $encoding)
Example:
<?php
$str1 = "Geeks";
print_r(mb_str_split($str1));
$str2 = "Welcome GfG";
print_r(mb_str_split($str2));
?>
Output:
Array
(
[0] => G
[1] => e
[2] => e
[3] => k
[4] => s
)
Array
(
[0] => W
[1] => e
[2] => l
[3] => c
[4] => o
[5] => m
[6] => e
[7] =>
[8] => G
[9] => f
[10] => G
)
Convert a String into an Array of Characters in PHP
Given a string, the task is to convert the string into an array of characters using PHP.
Examples:
Input: str = "GFG" Output: Array( [0] => G [1] => f [2] => G ) Input: str = "Hello Geeks" Output: Array( [0] => H [1] => e [2] => l [3] => l [4] => o [5] => [6] => G [7] => e [8] => e [9] => k [10] => s )
There are two methods to convert strings to an array of characters, these are:
Table of Content
- Using str_split() Function
- Using preg_split() Function
- Using mb_str_split() Function