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

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

Jun 13, 2016 am 11:53 AM
php介绍à propos匹配方法正则汉字de表达式ce

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>

Déclaration
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn

Outils d'IA chauds

Undresser.AI Undress

Undresser.AI Undress

Application basée sur l'IA pour créer des photos de nu réalistes

AI Clothes Remover

AI Clothes Remover

Outil d'IA en ligne pour supprimer les vêtements des photos.

Undress AI Tool

Undress AI Tool

Images de déshabillage gratuites

Clothoff.io

Clothoff.io

Dissolvant de vêtements AI

AI Hentai Generator

AI Hentai Generator

Générez AI Hentai gratuitement.

Article chaud

R.E.P.O. Crystals d'énergie expliqués et ce qu'ils font (cristal jaune)
3 Il y a quelques semainesBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Meilleurs paramètres graphiques
3 Il y a quelques semainesBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Comment réparer l'audio si vous n'entendez personne
3 Il y a quelques semainesBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: Comment déverrouiller tout dans Myrise
3 Il y a quelques semainesBy尊渡假赌尊渡假赌尊渡假赌

Outils chauds

Version crackée d'EditPlus en chinois

Version crackée d'EditPlus en chinois

Petite taille, coloration syntaxique, ne prend pas en charge la fonction d'invite de code

Bloc-notes++7.3.1

Bloc-notes++7.3.1

Éditeur de code facile à utiliser et gratuit

Listes Sec

Listes Sec

SecLists est le compagnon ultime du testeur de sécurité. Il s'agit d'une collection de différents types de listes fréquemment utilisées lors des évaluations de sécurité, le tout en un seul endroit. SecLists contribue à rendre les tests de sécurité plus efficaces et productifs en fournissant facilement toutes les listes dont un testeur de sécurité pourrait avoir besoin. Les types de listes incluent les noms d'utilisateur, les mots de passe, les URL, les charges utiles floues, les modèles de données sensibles, les shells Web, etc. Le testeur peut simplement extraire ce référentiel sur une nouvelle machine de test et il aura accès à tous les types de listes dont il a besoin.

MinGW - GNU minimaliste pour Windows

MinGW - GNU minimaliste pour Windows

Ce projet est en cours de migration vers osdn.net/projects/mingw, vous pouvez continuer à nous suivre là-bas. MinGW : un port Windows natif de GNU Compiler Collection (GCC), des bibliothèques d'importation et des fichiers d'en-tête librement distribuables pour la création d'applications Windows natives ; inclut des extensions du runtime MSVC pour prendre en charge la fonctionnalité C99. Tous les logiciels MinGW peuvent fonctionner sur les plates-formes Windows 64 bits.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Puissant environnement de développement intégré PHP