Let me introduce to you a few small examples of using regular expressions to match Chinese characters in PHP for your reference. The examples are quite good. Friends who are interested can refer to them.
php regular matching Chinese characters! /^[x{4e00}-x{9fa5}]+$/u Using regular matching will be different in different encodings and different programming languages. It should be understood and applied correctly, otherwise not only will you not get the desired results, but you will also be full of errors. 1. An example of utf-8 encoding: <?php $str = "汉字"; if (preg_match("/^[\x{4e00}-\x{9fa5}]+$/u",$str)) { print("该字符串全部是中文"); } else { print("该字符串不全部是中文"); } //by http://bbs.it-home.org ?> Copy after login 2. An example containing gbk, gb2312: <?php $action = trim($_GET['action']); if($action == "sub") { $str = $_POST['dir']; //if(!preg_match("/^[".chr(0xa1)."-".chr(0xff)."A-Za-z0-9_]+$/",$str)) //GB2312汉字字母数字下划线正则表达式 if(!preg_match("/^[\x{4e00}-\x{9fa5}A-Za-z0-9_]+$/u",$str)) //UTF-8汉字字母数字下划线正则表达式 { echo "<font color=red>您输入的[".$str."]含有违法字符</font>"; } else { echo "<font color=green>您输入的[".$str."]完全合法,通过!</font>"; } } ?> <form method="POST" action="?action=sub"> 输入字符(数字,字母,汉字,下划线): <input type="text" name="dir" value=""> <input type="submit" value="提交"> </form> Copy after login I suggest you try it yourself and see what the effect is? PHP regular matching of English is relatively simple, please pay more attention when matching Chinese characters. |