Home > php教程 > php手册 > 关于php正则匹配汉字的方法介绍

关于php正则匹配汉字的方法介绍

WBOY
Release: 2016-06-13 11:53:59
Original
1003 people have browsed it

php正则匹配汉字!

/^[\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">您输入的["</font></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)">"]含有违法字符"</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">您输入的["</font></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)">"]完全合法,通过!"</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>

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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template