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

Detailed explanation of the method of matching Chinese with php and javascript

怪我咯
怪我咯Original
2017-07-04 11:58:031366browse

This article mainly introduces the method of regular matching Chinese in PHP and javascript, and analyzes the Chinese operation skills of PHP and JavaScript regular matching in the case of 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 = "PHP中文网";
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 Detailed explanation of 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