Home > Article > Backend Development > Share the method of matching Chinese with php and javascript
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 '<pre class="brush:php;toolbar:false">'; print_r($arr); } else { print("该字符串不全部是中文"); echo '<pre class="brush:php;toolbar:false">'; 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!