中文匹配汉字多次失败

发布于 2021-11-10 20:02:41 字数 260 浏览 813 评论 4

$str = '中国';

preg_match_all($pattern,$str,$out); //这个这则匹配汉字,很艰难

$pattern_1 = '/[[x80-xff]]/';

$pattern_2 = '/[x4e00-x9fa5]/';

print_r($out);  //汉字部分都是乱码

编码格式是gbk,还是utf-8,都测试都无效.

 

 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

女中豪杰 2021-11-11 20:52:51

这个我已经解决了,还是感谢下,哈~

我后来查了下,相关版本 比如: [u4e00-u9fa5]    [x{4e00}-x{9fa5}]/u  ,关于正则匹配的问题涉及到编码问题,还是需要细细考虑下。

over。

白龙吟 2021-11-11 20:50:39
$str ='中国';
$result = preg_match("/[x{4e00}-x{9fa5}]/u",$str); 
print_r($result);

php里面是这样的

小情绪 2021-11-11 19:30:54
function str_cut($string, $length, $dot = '...')
{
    $strlen = strlen($string);
    if($strlen <= $length) return $string;
    $string = str_replace(array(' ', '&', '"', ''', '“', '”', '—', '<', '>', '·', '…'), array(' ', '&', '"', "'", '“', '”', '—', '<', '>', '·', '…'), $string);
    $strcut = '';
    if(strtolower(CHARSET) == 'utf-8')
    {
        $n = $tn = $noc = 0;
        while($n < $strlen)
        {
            $t = ord($string[$n]);
            if($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) {
                $tn = 1; $n++; $noc++;
            } elseif(194 <= $t && $t <= 223) {
                $tn = 2; $n += 2; $noc += 2;
            } elseif(224 <= $t && $t < 239) {
                $tn = 3; $n += 3; $noc += 2;
            } elseif(240 <= $t && $t <= 247) {
                $tn = 4; $n += 4; $noc += 2;
            } elseif(248 <= $t && $t <= 251) {
                $tn = 5; $n += 5; $noc += 2;
            } elseif($t == 252 || $t == 253) {
                $tn = 6; $n += 6; $noc += 2;
            } else {
                $n++;
            }
            if($noc >= $length) break;
        }
        if($noc > $length) $n -= $tn;
        $strcut = substr($string, 0, $n);
    }
    else
    {
        $dotlen = strlen($dot);
        $maxi = $length - $dotlen - 1;
        for($i = 0; $i < $maxi; $i++)
        {
            $strcut .= ord($string[$i]) > 127 ? $string[$i].$string[++$i] : $string[$i];
        }
    }
    $strcut = str_replace(array('&', '"', "'", '<', '>'), array('&', '"', ''', '<', '>'), $strcut);
    return $strcut.$dot;

}

 

这个函数是phpcms 2008 中的截取字符串的函数,写的有点多,所以我希望要找个正则匹配的,简单好维护,呵呵

反目相谮 2021-11-11 02:19:34
[^u4e00-u9fa5]

这是一段匹配除了中文以外的字符串

http://regexpal.com/ 测试

http://unicodelookup.com (资料)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文