Home > Backend Development > PHP Tutorial > 模仿PHP str_split分割汉字成数组的方法函数

模仿PHP str_split分割汉字成数组的方法函数

WBOY
Release: 2016-06-20 13:03:21
Original
1076 people have browsed it

array str_split ( string $string [, int $split_length = 1 ] )

str_split返回的是一个数组,第一个参数是string类型表示要分组的字符串,第二个参数是init类型,表示按几个字符来分组,如果没有第二个参数,那么默认按1个字符来分组。

但是对于汉字等字符串就不适合了,下面是分割汉字成数组的方法

header("Content-type:text/html;charset=utf-8");
$str="赵钱孙";
function mbstringToArray($str,$charset) {
    $strlen=mb_strlen($str);
    while($strlen){
        $array[]=mb_substr($str,0,1,$charset);
        $str=mb_substr($str,1,$strlen,$charset);
        $strlen=mb_strlen($str);
    }
    return $array;
}
//用法gbk utf-8
$arr = mbstringToArray($str,"utf-8");
 
var_dump($arr);
Copy after login

 


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template