PHP_PHP チュートリアルで中国語の文字を正規に一致させる方法の紹介

WBOY
リリース: 2016-07-21 15:11:52
オリジナル
925 人が閲覧しました

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>您输入的["</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>

www.bkjia.comtru​​ehttp://www.bkjia.com/PHPjc/326790.html技術記事 PHP の正規表現は中国語の文字に一致します! /^[x{4e00}-x{9fa5}]+$/u 上記の正規表現は、多くの PHP プログラマーを悩ませてきた中国語の文字に一致する正規表現です。 、しかしそれは実用的です..
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!