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

php使用正则验证中文_PHP

WBOY
Release: 2016-05-27 10:34:18
Original
951 people have browsed it

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 '字符串不全是中文';
}

Copy after login

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

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

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template