Home  >  Article  >  php教程  >  php使用正则验证中文,

php使用正则验证中文,

WBOY
WBOYOriginal
2016-06-13 08:41:441185browse

php使用正则验证中文,

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编码下都进行了一番测试,均可以使用。

您可能感兴趣的文章:

  • PHP 正则表达式验证中文的问题
  • php正则表达匹配中文问题分析小结
  • php用正则表达式匹配中文实例详解
  • PHP 正则判断中文UTF-8或GBK的思路及具体实现
  • PHP把空格、换行符、中文逗号等替换成英文逗号的正则表达式
  • php+正则将字符串中的字母数字和中文分割
  • PHP中使用正则表达式提取中文实现笔记
  • 正确的PHP匹配UTF-8中文的正则表达式
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