php中文汉字正则验证
php中文汉字正则验证
在javascript中,要判断字符串是中文是很简单的。比如:
var str = "php编程";
if (/^[\u4e00-\u9fa5]+$/.test(str)) {
alert("该字符串全部是中文");
} else {
alert("该字符串不全部是中文");
}
想当然的,在php中来判断字符串是否为中文,就会沿袭这个思路:
$str = "php编程";
if (preg_match("/^[\u4e00-\u9fa5]+$/",$str)) {
print("该字符串全部是中文");
} else {
print("该字符串不全部是中文");
}
?>
不过,很快就会发现,php并不支持这样的表达,报错:
Warning: preg_match() [function.preg-match]: Compilation failed: PCRE does not support \L, \l, \N, \U, or \u at offset 3 in test.php on line 3
刚开始从google上查了很多次,想从php正则表达式对于十六进制数据的表达方式上进行突破,发现在php中,是用\x表示十六进制数据的。于是,变换成如下的代码:
$str = "php编程";
if (preg_match("/^[\x4e00-\x9fa5]+$/",$str)) {
print("该字符串全部是中文");
} else {
print("该字符串不全部是中文");
}
貌似不报错了,判断的结果也正确,不过把$str换成“编程”两字,结果却还是显示“该字符串不全部是中文”,看来这样的判断还是不够准确。
后来跑回百度搜“php 匹配汉字 utf 8”,发现文章的匹配程度竟然要比google的高多了,看来百度的“百度更懂中文”还在一定程度上是正确的。在第二篇文章《★★★ 求UTF8下匹配汉字的正则, 在线等.........》中看到了如下的一些内容:
楼主zhiin(┈ Jcan ┈)2006-11-15 15:59:30 在 Web 开发 / PHP 提问
求UTF8下匹配汉字的正则, 不包括全角字符及特殊符号!
网上只能找到匹配全角字符的正则: ^[\x80-\xff]*^/
[\u4e00-\u9fa5]可以匹配中文,但是PHP又不支持
郁闷中.......
1 楼PleaseDoTellMeWhy(Allah bless you!)回复于 2006-11-15 16:04:55 得分 11
chr(0xa1) . '-' . chr(0xff)可以匹配所有中文,但是不知道在UTF-8下如何!Top
2 楼zhiin(┈ Jcan ┈)回复于 2006-11-15 16:11:34 得分 0
即使在gb2312下, chr(0xa1) . '-' . chr(0xff) 也不对
它把全角符号也匹配进来了Top
3 楼xuzuning(唠叨)回复于 2006-11-15 16:19:56 得分 90
模式修正符: u
按照这几位提供的线索逐个试了一下,发现还真的如他们所说,可能还跟编码有关系,因此需要了解一下模式修正符的相关知识——于是继续搜索百度。
在一篇《模式修正符》的文章中了解到:
u (PCRE_UTF8)
此修正符启用了一个 PCRE 中与 Perl 不兼容的额外功能。模式字符串被当成 UTF-8。本修正符在 Unix 下自 PHP 4.1.0 起可用,在 win32 下自 PHP 4.2.3 起可用。
例子:
preg_match('/[\x{2460}-\x{2468}]/u', $str); 匹配 内码汉字
按照他提供的方式进行测试,代码如下:
$str = "php编程";
if (preg_match("/^[\x{2460}-\x{2468}]+$/u",$str)) {
print("该字符串全部是中文");
} else {
print("该字符串不全部是中文");
}
发现这次依然对是否为中文判断失常。不过,既然\x表示的十六进制数据,为什么和js里边提供的范围\x4e00-\x9fa5不一样呢?于是我就换成了下边的代码:
$str = "php编程";
if (preg_match("/^[\x4e00-\x9fa5]+$/u",$str)) {
print("该字符串全部是中文");
} else {
print("该字符串不全部是中文");
}
本来以为铁定成功了的事情,没想到,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编程";
if (preg_match("/^[\x{4e00}-\x{9fa5}]+$/u",$str)) {
print("该字符串全部是中文");
} else {
print("该字符串不全部是中文");
}
知道了php中utf-8编码下用正则表达式匹配汉字的最终正确表达式——/^[\x{4e00}-\x{9fa5}]+$/u,于是我又用这个表达式去百度搜索,发现竟然还真有别人得出过这样正确的结论,只不过通过常规的方式很难找到而已,而且仅仅搜到有一篇——《用正则删除汉字》,看来互联网上对于信息的正确性的筛选还是亟待加强的。
ps:对google不死心,也搜索了一下,又发现了一篇文章《php常用类》,还是在百度空间的,呵呵,有意思!
AD:真正免费,域名+虚机+企业邮箱=0元

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Learn about Python programming with introductory code examples Python is an easy-to-learn, yet powerful programming language. For beginners, it is very important to understand the introductory code examples of Python programming. This article will provide you with some concrete code examples to help you get started quickly. Print HelloWorldprint("HelloWorld") This is the simplest code example in Python. The print() function is used to output the specified content

"Go Language Programming Examples: Code Examples in Web Development" With the rapid development of the Internet, Web development has become an indispensable part of various industries. As a programming language with powerful functions and superior performance, Go language is increasingly favored by developers in web development. This article will introduce how to use Go language for Web development through specific code examples, so that readers can better understand and use Go language to build their own Web applications. 1. Simple HTTP Server First, let’s start with a

PHP variables store values during program runtime and are crucial for building dynamic and interactive WEB applications. This article takes an in-depth look at PHP variables and shows them in action with 10 real-life examples. 1. Store user input $username=$_POST["username"];$passWord=$_POST["password"]; This example extracts the username and password from the form submission and stores them in variables for further processing. 2. Set the configuration value $database_host="localhost";$database_username="username";$database_pa

How to use PHP to write the inventory management function code in the inventory management system. Inventory management is an indispensable part of many enterprises. For companies with multiple warehouses, the inventory management function is particularly important. By properly managing and tracking inventory, companies can allocate inventory between different warehouses, optimize operating costs, and improve collaboration efficiency. This article will introduce how to use PHP to write code for inventory warehouse management functions, and provide you with relevant code examples. 1. Establish the database before starting to write the code for the inventory warehouse management function.

The simplest code example of Java bubble sort Bubble sort is a common sorting algorithm. Its basic idea is to gradually adjust the sequence to be sorted into an ordered sequence through the comparison and exchange of adjacent elements. Here is a simple Java code example that demonstrates how to implement bubble sort: publicclassBubbleSort{publicstaticvoidbubbleSort(int[]arr){int

Huawei Cloud Edge Computing Interconnection Guide: Java Code Samples to Quickly Implement Interfaces With the rapid development of IoT technology and the rise of edge computing, more and more enterprises are beginning to pay attention to the application of edge computing. Huawei Cloud provides edge computing services, providing enterprises with highly reliable computing resources and a convenient development environment, making edge computing applications easier to implement. This article will introduce how to quickly implement the Huawei Cloud edge computing interface through Java code. First, we need to prepare the development environment. Make sure you have the Java Development Kit installed (

Title: From Beginner to Mastery: Code Implementation of Commonly Used Data Structures in Go Language Data structures play a vital role in programming and are the basis of programming. In the Go language, there are many commonly used data structures, and mastering the implementation of these data structures is crucial to becoming a good programmer. This article will introduce the commonly used data structures in the Go language and give corresponding code examples to help readers from getting started to becoming proficient in these data structures. 1. Array Array is a basic data structure, a group of the same type

Java Selection Sorting Method Code Writing Guide and Examples Selection sorting is a simple and intuitive sorting algorithm. The idea is to select the smallest (or largest) element from the unsorted elements each time and exchange it until all elements are sorted. This article will provide a code writing guide for selection sorting, and attach specific Java sample code. Algorithm Principle The basic principle of selection sort is to divide the array to be sorted into two parts, sorted and unsorted. Each time, the smallest (or largest) element is selected from the unsorted part and placed at the end of the sorted part. Repeat the above
