Home  >  Article  >  php教程  >  php如何判断字符串是否含有中文(附代码)

php如何判断字符串是否含有中文(附代码)

WBOY
WBOYOriginal
2016-06-13 09:28:403165browse

这篇文章主要介绍了php判断字符串是否含有中文的方法,有一定的参考价值,感兴趣的朋友可以参考一下。

php中判断字符串是否全是中文或含有中文的实现代码,主要是利用正则匹配中文判定。

一,判断全是中文

代码如下:

$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 "没有中文"; 
}

我这几种方法都是在utf-8下面测试的,别的编码下没有测试过。

【相关教程推荐】

1. php编程从入门到精通全套视频教程 

2. php从入门到精通  

3. bootstrap教程

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