关于英汉混合字符串的分隔方法

//把字符转化为数组自动识别中英文
function math($string, $code = 'UTF-8')
{
    if ($code == 'UTF-8') {
        $pa = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/";
    } else {
        $pa = "/[\x01-\x7f]|[\xa1-\xff][\xa1-\xff]/";
    }
    preg_match_all($pa, $string, $t_string);
    $math = "";
    foreach ($t_string[0] as $k => $s) {
        $math[] = $s;
    }
    return $math;
}

$str='我爱北京 love you 123';
$arr=math($str);

 效果如下图


posted @ 2016-11-07 17:52  雪后西溏  阅读(391)  评论(0编辑  收藏  举报