美文网首页工作生活
php拆分中文字符串

php拆分中文字符串

作者: 诗无尽头i | 来源:发表于2019-07-03 19:22 被阅读0次

拆分字符串函数,只支持 gb2312编码
代码来源于网络,时间很久了,请作者私信我,将注明版权

<?php
function arr_split_zh($tempaddtext) {
    $tempaddtext = iconv("UTF-8", "GBK//IGNORE", $tempaddtext);
    $cind = 0;
    $arr_cont = array();
    for ($i = 0; $i < strlen($tempaddtext); $i++) {
        if (strlen(substr($tempaddtext, $cind, 1)) > 0) {
            if (ord(substr($tempaddtext, $cind, 1)) < 0xA1) { //如果为英文则取1个字节
                array_push($arr_cont, substr($tempaddtext, $cind, 1));
                $cind++;
            } else {
                array_push($arr_cont, substr($tempaddtext, $cind, 2));
                $cind+= 2;
            }
        }
    }
    foreach ($arr_cont as & $row) {
        $row = iconv("gb2312", "UTF-8", $row);
    }
    var_dump($arr_cont);
}
arr_split_zh('诗无尽头 - 唯有自由才能触摸虚无的自我。');

输出结果

Array ( 
[0] => 诗
[1] => 无
[2] => 尽
[3] => 头
[4] => 
[5] => - 
[6] => 
[7] => 唯
[8] => 有
[9] => 自
[10] => 由
[11] => 才
[12] => 能
[13] => 触
[14] => 摸
[15] => 虚
[16] => 无
[17] => 的
[18] => 自
[19] => 我
[20] => 。
)

相关文章

网友评论

    本文标题:php拆分中文字符串

    本文链接:https://www.haomeiwen.com/subject/ehzchctx.html