Home >Backend Development >PHP Tutorial >php使用正则验证中文_PHP

php使用正则验证中文_PHP

WBOY
WBOYOriginal
2016-05-27 10:34:18943browse

php用preg_match来匹配并判断一个字符串中是否含有中文或者都是中文的方法如下:

$str = 'php学习博客';
if(preg_match('/[\x7f-\xff]/', $str)){
 echo '字符串中有中文<br/>';
}else{
 echo '字符串中没有中文<br/>';
}

if(preg_match('/^[\x7f-\xff]+$/', $str)){
 echo '字符串全是中文';
}else{
 echo '字符串不全是中文';
}

以上程序的输出的结果为:

字符串中有中文
字符串不全是中文

utf-8和gbk编码下都进行了一番测试,均可以使用。

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