/^[\x{4e00}-\x{9fa5}]+$/u
以上这个正则表达式就是困扰了很多php程序员的匹配汉字的正则表达式
大家可能会觉得很简单,实际上不同编码,不同程序语言,都有些细微的出入,稍不注意就得不到正确的结果。
下面是utf-8编码的例子:<span style="COLOR: rgb(0,0,187)">$str </span><span style="COLOR: rgb(0,119,0)">= </span><span style="COLOR: rgb(221,0,0)">"汉字"</span><span style="COLOR: rgb(0,119,0)">;<br>if (</span><span style="COLOR: rgb(0,0,187)">preg_match</span><span style="COLOR: rgb(0,119,0)">(</span><span style="COLOR: rgb(221,0,0)">"/^[\x{4e00}-\x{9fa5}]+$/u"</span><span style="COLOR: rgb(0,119,0)">,</span><span style="COLOR: rgb(0,0,187)">$str</span><span style="COLOR: rgb(0,119,0)">)) {<br>print(</span><span style="COLOR: rgb(221,0,0)">"该字符串全部是中文"</span><span style="COLOR: rgb(0,119,0)">);<br>} else {<br>print(</span><span style="COLOR: rgb(221,0,0)">"该字符串不全部是中文"</span><span style="COLOR: rgb(0,119,0)">);<br>} <br></span>
下面的例子包含gbk,gb2312的<span style="COLOR: rgb(0,0,187)"><span style="FONT-FAMILY: Arial; COLOR: rgb(0,0,0)">例子:</span></span>
<span style="COLOR: rgb(0,0,187)"><?php<BR>$action </SPAN><SPAN style="COLOR: rgb(0,119,0)">= </SPAN><SPAN style="COLOR: rgb(0,0,187)">trim</SPAN><SPAN style="COLOR: rgb(0,119,0)">(</SPAN><SPAN style="COLOR: rgb(0,0,187)">$_GET</SPAN><SPAN style="COLOR: rgb(0,119,0)">[</SPAN><SPAN style="COLOR: rgb(221,0,0)">'action'</SPAN><SPAN style="COLOR: rgb(0,119,0)">]);<BR>if(</SPAN><SPAN style="COLOR: rgb(0,0,187)">$action </SPAN><SPAN style="COLOR: rgb(0,119,0)">== </SPAN><SPAN style="COLOR: rgb(221,0,0)">"sub"</SPAN><SPAN style="COLOR: rgb(0,119,0)">)<BR>{<BR> </SPAN><SPAN style="COLOR: rgb(0,0,187)">$str </SPAN><SPAN style="COLOR: rgb(0,119,0)">= </SPAN><SPAN style="COLOR: rgb(0,0,187)">$_POST</SPAN><SPAN style="COLOR: rgb(0,119,0)">[</SPAN><SPAN style="COLOR: rgb(221,0,0)">'dir'</SPAN><SPAN style="COLOR: rgb(0,119,0)">]; <BR> </SPAN><SPAN style="COLOR: rgb(255,128,0)">//if(!preg_match("/^[".chr(0xa1)."-".chr(0xff)."A-Za-z0-9_]+$/",$str)) //GB2312汉字字母数字下划线正则表达式 <BR> </SPAN><SPAN style="COLOR: rgb(0,119,0)">if(!</SPAN><SPAN style="COLOR: rgb(0,0,187)">preg_match</SPAN><SPAN style="COLOR: rgb(0,119,0)">(</SPAN><SPAN style="COLOR: rgb(221,0,0)">"/^[\x{4e00}-\x{9fa5}A-Za-z0-9_]+$/u"</SPAN><SPAN style="COLOR: rgb(0,119,0)">,</SPAN><SPAN style="COLOR: rgb(0,0,187)">$str</SPAN><SPAN style="COLOR: rgb(0,119,0)">)) </SPAN><SPAN style="COLOR: rgb(255,128,0)">//UTF-8汉字字母数字下划线正则表达式<BR> </SPAN><SPAN style="COLOR: rgb(0,119,0)">{ <BR> echo </SPAN><SPAN style="COLOR: rgb(221,0,0)">"<font color=red>您输入的["</span><span style="COLOR: rgb(0,119,0)">.</span><span style="COLOR: rgb(0,0,187)">$str</span><span style="COLOR: rgb(0,119,0)">.</span><span style="COLOR: rgb(221,0,0)">"]含有违法字符</font>"</span><span style="COLOR: rgb(0,119,0)">; <br> }<br> else <br> {<br> echo </span><span style="COLOR: rgb(221,0,0)">"<font color=green>您输入的["</span><span style="COLOR: rgb(0,119,0)">.</span><span style="COLOR: rgb(0,0,187)">$str</span><span style="COLOR: rgb(0,119,0)">.</span><span style="COLOR: rgb(221,0,0)">"]完全合法,通过!</font>"</span><span style="COLOR: rgb(0,119,0)">; <br> }<br>}<br></span><span style="COLOR: rgb(0,0,187)">?><br></span><form method="POST" action="?action=sub"><br>输入字符(数字,字母,汉字,下划线):<br> <input type="text" name="dir" value=""><br> <input type="submit" value="提交"><br></form>