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

怪我咯
Release: 2023-03-12 14:34:01
Original
1341 people have browsed it

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);
}
Copy after login

Regular match in php gbk, gb2312 Chinese:

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

javascript regular match Chinese:

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

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!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!