您的位置:首页 > 编程语言 > PHP开发

UTF-8编码时,PHP如何正则匹配中文汉字?亲测可用

2017-08-08 10:34 1031 查看
这个方法亲测可用,代码如下:

<?php
header('content-type:text/html;charset=utf-8');
$input = "^_^,PHP is the best programming language in the world!";
if (preg_match_all("/([\x{4e00}-\x{9fa5}]+)/u", $input, $match))
{
var_dump($match);
print("该字符串含有中文");
}
else {
print("该字符串没有中文");
}


效果如图:



<?php
header('content-type:text/html;charset=utf-8');
$input = "utf-8编码时,如何正则匹配中文汉字?";
if (preg_match_all("/([\x{4e00}-\x{9fa5}]+)/u", $input, $match))
{
var_dump($match);
print("该字符串含有中文");
}
else {
print("该字符串没有中文");
}

效果截图:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: