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); }
例子:
$s = 'abc你好吗defgh'; $strArr = str_split_unicode($s); print_r($strArr);
输出:Array ( [0] => a [1] => b [2] => c [3] => 你 [4] => 好 [5] => 吗 [6] => d [7] => e [8] => f [9] => g [10] => h )
例子2:
$s = 'abc你好吗defgh'; $strArr = str_split_unicode($s,3); print_r($strArr);
输出:Array ( [0] => abc [1] => 你好吗 [2] => def [3] => gh )