PHP将一个字符串转换成数组,支持中文
/**
* 将一个字符串转换成数组,支持中文
* @param string $string 待转换成数组的字符串
* @return string 转换后的数组
*/
function strToArray($string) {
$strlen = mb_strlen($string);
while ($strlen) {
$array[] = mb_substr($string, 0, 1, "utf8");
$string = mb_substr($string, 1, $strlen, "utf8");
$strlen = mb_strlen($string);
}
return $array;
}
用法:
$string = '这里就是要转换成数组的字符串,www.web.xin';
$arr = strToArray($string);
print_r($arr);
输出:
Array(
[0] => 这
[1] => 里
[2] => 就
[3] => 是
[4] => 要
[5] => 转
[6] => 换
[7] => 成
[8] => 数
[9] => 组
[10] => 的
[11] => 字
[12] => 符
[13] => 串
[14] => ,
[15] => w
[16] => w
[17] => w
[18] => .
[19] => w
[20] => e
[21] => b
[22] => 3
[23] => .
[24] => x
[25] => i
[26] => n
)