search
Homephp教程php手册php正则表达匹配中文问题分析,

php正则表达匹配中文问题分析,

Jun 13, 2016 am 08:44 AM
phpstrChineseanalyzematchregularquestion

php正则表达匹配中文问题分析,

$str = '中华人民共和国123456789abcdefg';<br />echo preg_match("/^[u4e00-u9fa5_a-zA-Z0-9]{3,15}$",$strName);



 

运行一下上面这段代码,看会有什么提示信息?
 
Warning: preg_match(): Compilation failed: PCRE does not support L, l, N, P, p, U, u, or X at offset 3 in F:http://www.hzhuti.com/nokia/5800/ on line 2
原来,PHP正则表达式中不支持下列 Perl 转义序列:L, l, N, P, p, U, u, or X

在 UTF-8 模式下,允许用“x{...}”,花括号中的内容是表示十六进制数字的字符串。

原来的十六进制转义序列 xhh 如果其值大于 127 的话则匹配了一个双字节 UTF-8 字符。
所以,
可以这样来解决

preg_match("/^[x80-xff_a-zA-Z0-9]{3,15}$",$strName);<br /><br /><br />preg_match('/[x{2460}-x{2468}]/u', $str);


 

匹配 内码汉字
按照他提供的方式进行测试,代码如下:

 代码如下 复制代码 

$str = "php编程";<br />if (preg_match("/^[x{2460}-x{2468}]+$/u",$str)) {<br />print("该字符串全部是中文");<br />} else {<br />print("该字符串不全部是中文");<br />}


 

发现这次依然对是否为中文判断失常。不过,既然x表示的十六进制数据,为什么和js里边提供的范围x4e00-x9fa5不一样呢?于是我就换成了下边的代码:

$str = "php编程";<br />if (preg_match("/^[x4e00-x9fa5]+$/u",$str)) {<br />print("该字符串全部是中文");<br />} else {<br />print("该字符串不全部是中文");<br />}


 

本来以为铁定成功了的事情,没想到,warning又一次产生了:
Warning: preg_match() [function.preg-match]: Compilation failed: invalid UTF-8 string at offset 6 in test.php on line 3

看来又有错误的表达方式了,于是对照了一下那篇文章的表达方式,给“4e00”和“9fa5”两边分别用"{"和“}”包起来,跑了一遍,发现真的准确了:

$str = "php编程";<br />if (preg_match("/^[x{4e00}-x{9fa5}]+$/u",$str)) {<br />print("该字符串全部是中文");<br />} else {<br />print("该字符串不全部是中文");<br />}<br /> 


知道了php中utf-8编码下用正则表达式匹配汉字的最终正确表达式——/^[x{4e00}-x{9fa5}]+$/u,

最后总结出

//if (preg_match(&ldquo;/^[".chr(0xa1)."-".chr(0xff)."]+$/&rdquo;, $str)) { //只能在GB2312情况下使用<br />if (preg_match(&ldquo;/^[x7f-xff]+$/&rdquo;, $str)) { //兼容gb2312,utf-8<br />echo &ldquo;正确输入&rdquo;;<br />} else {<br />echo &ldquo;错误输入&rdquo;;<br />}


 

双字节字符编码范围

1. GBK (GB2312/GB18030)
x00-xff GBK双字节编码范围
x20-x7f ASCII
xa1-xff 中文 gb2312
x80-xff 中文 gbk

2. UTF-8 (Unicode)

u4e00-u9fa5 (中文)
x3130-x318F (韩文
xAC00-xD7A3 (韩文)
u0800-u4e00 (日文)

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment