Home  >  Article  >  Backend Development  >  Implementation code to determine whether a string is all Chinese or contains Chinese in PHP

Implementation code to determine whether a string is all Chinese or contains Chinese in PHP

高洛峰
高洛峰Original
2016-11-30 13:51:421195browse

First, the judgment is all in Chinese
The code is as follows:
$str="'324 is";
if(!eregi("[^x80-xff]","$str")){
echo "All in Chinese" ;
}else{
echo "is not";
}

Second, determine whether it contains Chinese
The code is as follows:
$str = "Chinese";
if (preg_match("/[x7f-xff]/", $str) ) {
echo "Contains Chinese";
}else{
echo "No Chinese";
}
or
$pattern = '/[^x00-x80]/';
if(preg_match($pattern,$str) ){
echo "Contains Chinese";
}else{
echo "No Chinese";
}

I tested these methods under utf-8 and have not tested them under other encodings.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn