In PHP, what is the best way to split a string into an array of Unicode characters? If the input is not necessarily UTF-8?
I want to know whether the set of Unicode
function str_split_unicode($str, $l = 0) {
if ($l > 0) {
$ret = array();
$len = mb_strlen($str, "UTF-8");
for ($i = 0; $i < $len; $i += $l) {
$ret[] = mb_substr($str, $i, $l, "UTF-8");
}
return $ret;
}
return preg_split("//u", $str, -1, PREG_SPLIT_NO_EMPTY);
}
var_dump(str_split_unicode("لأآأئؤة"));
output:
array (size=7)
0 => string 'ل' (length=2)
1 => string 'أ' (length=2)
2 => string 'آ' (length=2)
3 => string 'أ' (length=2)
4 => string 'ئ' (length=2)
5 => string 'ؤ' (length=2)
6 => string 'ة' (length=2)
for more information : http://php.net/manual/en/function.str-split.php