Apr
26
2020
php匹配中文及生僻字
作者:
绝缘体.. 发布:
2020-04-26 07:17 分类:
未分类 阅读:
抢沙发
php基本汉字的unicode编码匹配在[\x{4e00}-\x{9fa5}]范围,如果要加上一些生僻字,补充汉字,扩展汉字就需要增加匹配的范围。unicode汉字编码范围参考。
//自动转UTF-8编码
$str = "鿊";
$encode = mb_detect_encoding($str, ["ASCII", 'UTF-8', "GB2312", "GBK", 'BIG5', 'LATIN1']);
if ($encode != 'UTF-8') {
$str = mb_convert_encoding($str, 'UTF-8', $encode);
}
$res = preg_match("/^([\x{4e00}-\x{9fa5}]|[\x{9fa6}-\x{9fef}]|[\x{3400}-\x{4db5}]|[\x{20000}-\x{2ebe0}]){2,15}$/u", $str);
if (!$res) {
echo "不是中文汉字";
}
微信扫一扫,打赏作者吧~