php判断是不是为中文正则表达式大全(转)
转载自?? http://www.cnblogs.com/DavidYan/articles/2032115.html
?
?
php判断是否为中文正则表达式大全
?
$str="aaa";
if(!eregi("[^\x80-\xff]","$str"))
{
echo "是";
}
else
{
echo "不是";
}
?>
$str = "中国";
echo $str;
echo "
";
//if (preg_match("/^[".chr(0xa1)."-".chr(0xff)."]+$/", $str)) {//只能在GB2312情况下使用
if (preg_match("/^[\x7f-\xff]+$/", $str)) { //兼容gb2312,utf-8
echo "正确输入";
} else {
echo "错误输入";
}
?>
如果想判断一个字符串内是否有含有中文,请用下面的代码:
if (preg_match("/[\x7f-\xff]/", $string)) {
echo "有中文";
}else{
echo "没有中文";
}
判断中文和编码有关gbk是双字节,utf8是三字节,可以根据中文的范围来判断???
?
编码范围1. GBK(GB2312/GB18030)???
\x00-\xffGBK双字节编码范围???
\x20-\x7fASCII???
\xa1-\xff中文???
\x80-\xff 中文??
??
2. UTF-8(Unicode)???
\u4e00-\u9fa5(中文)???
\x3130-\x318F(韩文???
\xAC00-\xD7A3(韩文)???
\u0800-\u4e00(日文)???
ps:韩文是大于[\u9fa5]的字符???
正则例子:???
preg_replace(”/([\x80-\xff])/”,”",$str);??
preg_replace(”/([u4e00-u9fa5])/”,”",$str);???
?
?
- //判断内容里有没有中文-GBK?(PHP)???????
- function?check_is_chinese($s){???????
- ?????return?preg_match('/[\x80-\xff]./',?$s);???????
- }???????
- ?????
- //获取字符串长度-GBK?(PHP)???????
- function?gb_strlen($str){???????
- ?????$count?=?0;???????
- ?????for($i=0;?$istrlen($str);?$i++){???????
- ?????????$s?=?substr($str,?$i,?1);???????
- ?????????if?(preg_match("/[\x80-\xff]/",?$s))?++$i;???????
- ???????????++$count;???????
- ?????}???????
- ?????return?$count;???????
- }???????
- ?????
- //截取字符串字串-GBK?(PHP)???????
- function?gb_substr($str,?$len){???????
- ?????$count?=?0;???????
- ?????for($i=0;?$istrlen($str);?$i++){???????
- ?????????if($count?==?$len)?break;???????
- ?????????if(preg_match("/[\x80-\xff]/",?substr($str,?$i,?1)))?++$i;???????
- ???????????++$count;???????????????
- ?????}???????
- ?????return?substr($str,?0,?$i);???????
- }???????
- ?????
- //统计字符串长度-UTF8?(PHP)???????
- function?utf8_strlen($str)?{???????
- ?????$count?=?0;???????
- ?????for($i?=?0;?$i?strlen($str);?$i++){???????
- ?????????$value?=?ord($str[$i]);???????
- ?????????if($value?>?127)?{???????
- ?????????????$count++;???????
- ?????????????if($value?>=?192?&&?$value?$i++;???????
- ?????????????elseif($value?>=?224?&&?$value?$i?=?$i?+?2;???????
- ?????????????elseif($value?>=?240?&&?$value?$i?=?$i?+?3;???????
- ?????????????else?die('Not?a?UTF-8?compatible?string');???????
- ?????????}???????
- ?????????$count++;???????
- ?????}???????
- ?????return?$count;???????
- }???????
- ?????
- ?????
- //截取字符串-UTF8(PHP)???????
- function?utf8_substr($str,$position,$length){???????
- ?????$start_position?=?strlen($str);???????
- ?????$start_byte?=?0;???????
- ?????$end_position?=?strlen($str);???????
- ?????$count?=?0;???????
- ?????for($i?=?0;?$i?strlen($str);?$i++){???????
- ?????????if($count?>=?$position?&&?$start_position?>?$i){???????
- ?????????????$start_position?=?$i;???????
- ?????????????$start_byte?=?$count;???????
- ?????????}???????
- ?????????if(($count-$start_byte)>=$length)?{???????
- ?????????????$end_position?=?$i;???????
- ?????????????break;???????
- ?????????}???????????
- ?????????$value?=?ord($str[$i]);???????
- ?????????if($value?>?127){???????
- ?????????????$count++;???????
- ?????????????if($value?>=?192?&&?$value?$i++;???????
- ?????????????elseif($value?>=?224?&&?$value?$i?=?$i?+?2;???????
- ?????????????elseif($value?>=?240?&&?$value?$i?=?$i?+?3;???????
- ?????????????else?die('Not?a?UTF-8?compatible?string');???????
- ?????????}???????
- ?????????$count++;???????
- ?????
- ?????}???????
- ?????return(substr($str,$start_position,$end_position-$start_position));???????
- }???????
- ?????
- //判断是否是有韩文-UTF-8?(JavaScript)???????
- function?checkKoreaChar(str)?{???????
-
?????for(i=0;?i
- ?????????if(((str.charCodeAt(i)?>?0x3130?&&?str.charCodeAt(i)?0x318F)?||?(str.charCodeAt(i)?>=?0xAC00?&&?str.charCodeAt(i)?
- ?????????????return?true;???????
- ?????????}???????
- ?????}???????
- ?????return?false;???????
- }???????
- ?????
- //判断是否有中文字符-GBK?(JavaScript)???????
- function?check_chinese_char(s){???????
- ?????return?(s.length?!=?s.replace(/[^\x00-\xff]/g,"**").length);???????
- }???
?
UTF-8匹配:
在javascript中,要判断字符串是中文是很简单的。比如:
var str = "php编程";
if (/^[\u4e00-\u9fa5]+$/.test(str)) {
alert("该字符串全部是中文");
}
else{
alert("该字符串不全部是中文");
}
php中,是用\x表示十六进制数据的。于是,变换成如下的代码:
$str = "php编程";
if (preg_match("/^[\x4e00-\x9fa5]+$/",$str)) {
print("该字符串全部是中文");
} else {
print("该字符串不全部是中文");
}
貌似不报错了,判断的结果也正确,不过把$str换成“编程”两字,结果却还是显示“该字符串不全部是中文”,看来这样的判断还是不够准确。
重要:查阅了发现,对于[\x4e00-\x9fa5]这块东西,自己做一个强化的解释
php的正则中, [\x4e00-\x9fa5],其实就是 字符和字符组的概念, \x{hex},表达一个16进制数,需要注意的是hex 可以是1-2位的,也可以是4位的,但是如果是4位的必须加上大括号,
同时,如果是大于x{FF}的hex,必须和u 修饰符连用,不然会非法出错
网上只能找到匹配全角字符的正则:??^[\x80-\xff]*^/???,这里可以不加大括号
[\u4e00-\u9fa5]可以匹配中文,但是PHP又不支持???
不过,既然\x表示的十六进制数据,为什么和js里边提供的范围\x4e00-\x9fa5不一样呢?于是我就换成了下边的代码,发现真的准确了:
$str = "php编程";
if (preg_match("/^[\x{4e00}-\x{9fa5}]+$/u",$str)) {
print("该字符串全部是中文");
} else {
print("该字符串不全部是中文");
}
知道了php中utf-8编码下用正则表达式匹配汉字的最终正确表达式——/^[\x{4e00}-\x{9fa5}]+$/u,
参考以上文章写了如下一段测试代码(复制以下代码保存成.php文件)
$action = trim($_GET['action']);
if($action == "sub")
{
??? $str =$_POST['dir'];???
???//if(!preg_match("/^[".chr(0xa1)."-".chr(0xff)."A-Za-z0-9_]+$/",$str))//GB2312汉字字母数字下划线正则表达式
???if(!preg_match("/^[\x{4e00}-\x{9fa5}A-Za-z0-9_]+$/u",$str))??//UTF-8汉字字母数字下划线正则表达式
???{??
???????echo "
??? }
??? else
??? {
???????echo "
??? }
}
?>
GBK:
preg_match("/^[".chr(0xa1)."-".chr(0xff)."A-Za-z0-9_]+$/",$str);//GB2312汉字字母数字下划线正则表达式
?
$str="aaa";
if(!eregi("[^\x80-\xff]","$str"))
{
echo "是";
}
else
{
echo "不是";
}
?>
$str = "中国";
echo $str;
echo "
";
//if (preg_match("/^[".chr(0xa1)."-".chr(0xff)."]+$/", $str)) {//只能在GB2312情况下使用
if (preg_match("/^[\x7f-\xff]+$/", $str)) { //兼容gb2312,utf-8
echo "正确输入";
} else {
echo "错误输入";
}
?>
如果想判断一个字符串内是否有含有中文,请用下面的代码:
if (preg_match("/[\x7f-\xff]/", $string)) {
echo "有中文";
}else{
echo "没有中文";
}
?
?
?
?
?
相关文章
相关视频