$str="'324是中国人"; if(!eregi("[^\x80-\xff]","$str")){ echo "全是中文"; }else{ echo "不是"; }
$str = "中文汉字"; if (preg_match("/[\x7f-\xff]/", $str)) { echo "含有中文"; }else{ echo "没有中文"; } 或 $pattern = '/[^\x00-\x80]/'; if(preg_match($pattern,$str)){ echo "含有中文"; }else{ echo "没有中文"; }
function getChinese($str,$charset='utf8'){
if($charset=='gb2312'){
if(!preg_match_all("/^[".chr(0xa1)."-".chr(0xff)."]+/",$str,$match)){
return false;
}
return implode('',$match[0]);
}
//
if($charset=='utf8'){
if(!preg_match_all("/[\x{4e00}-\x{9fa5}]+/u",$str,$match)){
return false;
}
return implode('',$match[0]);
}
return false;
}
2)gb2312的编码格式,匹配中文字符串代码如下:<?php
$str = "utf-8下匹配出中文字符串";
$preg = "/[\x{4e00}-\x{9fa5}]+/u";
if(preg_match_all($preg,$str,$matches)){
print_r($matches);
}
?>
<?php
$str = "gb2312下匹配出中文字符串";
$preg = "/([".chr(0xb0)."-".chr(0xf7)."][".chr(0xa1)."-".chr(0xfe)."])+/i";
if(preg_match($preg,$str,$matches)){
print_r($matches);
}
?>
如对本文有疑问,请提交到交流论坛,广大热心网友会为你解答!! 点击进入论坛