Home > php教程 > PHP源码 > php字符串转数组,支持中文

php字符串转数组,支持中文

PHP中文网
Release: 2016-05-25 17:06:31
Original
1345 people have browsed it

php字符串转数组,支持中文

PHP代码

/**
 * 把字符串转成数组,支持汉字,只能是utf-8格式的
 * @param $str
 * @return array
 */
function StringToArray($str)
{
    $result = array();
    $len = strlen($str);
    $i = 0;
    while($i < $len){
        $chr = ord($str[$i]);
        if($chr == 9 || $chr == 10 || (32 <= $chr && $chr <= 126)) {
            $result[] = substr($str,$i,1);
            $i +=1;
        }elseif(192 <= $chr && $chr <= 223){
            $result[] = substr($str,$i,2);
            $i +=2;
        }elseif(224 <= $chr && $chr <= 239){
            $result[] = substr($str,$i,3);
            $i +=3;
        }elseif(240 <= $chr && $chr <= 247){
            $result[] = substr($str,$i,4);
            $i +=4;
        }elseif(248 <= $chr && $chr <= 251){
            $result[] = substr($str,$i,5);
            $i +=5;
        }elseif(252 <= $chr && $chr <= 253){
            $result[] = substr($str,$i,6);
            $i +=6;
        }
    }
    return $result;
}
Copy after login

                   


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
Latest Articles by Author
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template