Home  >  Article  >  Backend Development  >  Share the method of matching Chinese with php and javascript

Share the method of matching Chinese with php and javascript

黄舟
黄舟Original
2017-03-17 09:20:571145browse

This article mainly introduces the method of regular matching Chinese with PHP and javascript, and analyzes the Chinese operation skills of PHP and JavaScript regular matching under UTF-8 and GBK encoding in the form of examples. What is needed Friends can refer to the following

The example in this article describes the method of matching Chinese with php and javascript. Share it with everyone for your reference, the details are as follows:

regular matching utf-8 Chinese in php: (the emphasis is: [\x{4e00}-\x{9fa5} ]+

$str = "脚本之家";
if (preg_match("/^[\x{4e00}-\x{9fa5}]+$/u",$str,$arr)) {
  print("该字符串全部是中文");
  echo &#39;<pre class="brush:php;toolbar:false">&#39;;
  print_r($arr);
} else {
  print("该字符串不全部是中文");
  echo &#39;<pre class="brush:php;toolbar:false">&#39;;
  print_r($arr);
}

Regular match in php gbk, gb2312 Chinese:

preg_match("/^[".chr(0xa1)."-".chr(0xff)."]+$/",$str)

javascript regular match Chinese:

var str = "php编程";
if (/^[\u4e00-\u9fa5]+$/.test(str)) {
alert("该字符串全部是中文");
} else {
alert("该字符串不全部是中文");
}

The above is the detailed content of Share the method of matching Chinese with php and javascript. For more information, please follow other related articles on the PHP Chinese website!

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