下面的例子包含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>