PHP中使用正则表达式提取中文实现笔记,
PHP中使用正则表达式提取中文实现笔记,
最近老板叫做一个数据查重的小练习,涉及从一个包含中文字段的文件中提取出其中的中文字段并存储,使用php开发。中间涉及到php正则表达式中文匹配的问题,网上搜罗一大片,但是也很乱没有一个准信儿,经过自己的代码的修改和检验,先将extract函数写下。
首先要注意到的是,双字节字符的编码问题,这里我们以后还可能会遇到像韩文、日文等编码问题,与中文理解上是一个意思。
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 (日文)
在Notepad++下面,我们可以首先进行测试我们的正则书写的错误与否。第一个表达式我是使用[\u4e00-\u9fa5]+来检验的,+号表示不止一个
匹配符。结果与预期相同,那么,是否在脚本中就可以使用这个正则了呢?
我们测试一下,我们使用preg_match_all(‘/[\u4e00-\u9fa5]+/', $subject,$matches)调用,然后你却看到了这么一个结果:Compilation failed: PCRE does not support \L, \l, \N{name}, \U, or \u at offset 2。。。。是不是很头大??这究竟是什么原因?
查阅了很多资料后发现,u (PCRE_UTF8),就是上面的PCRE,这是是一个Perl库,包括 perl 兼容的正规表达式库。此修正符启用了一个 PCRE 中与 Perl 不兼容的额外功能。模式字符串被当成 UTF-8。本修正符在 Unix 下自 PHP 4.1.0 起可用,在 win32 下自 PHP 4.2.3 起可用。而php正则表达式对于十六进制数据的表达方式上也有所不同,在php中,是用\x表示十六进制数据的。下面我们就将代码优化一下,检测函数变为:
复制代码 代码如下:
class storeDataAdapter extends Store{
private $dsData;
/**
* 数据转换函数,调用preg_match_all根据$pattern正则来进行数值匹配,并将返回的结果以数组形式存储在$matches中,
* $matches[0]将包含与整个模式匹配的文本,$matches[1] 将包含与第一个捕获的括号中的子模式所匹配的文本,以此类推
* @see Store::data_convert()
*/
public function data_convert($pattern,$subject) {
$matches=array();
if (preg_match_all($pattern, $subject,$matches)){
return $matches[0];
}else
{
return null;
}
}
}
调用的时候变为:
复制代码 代码如下:
$store=new storeDataAdapter($txtContent);
$match=array();
$dsName=$store->data_convert(‘/[\x7f-\xff]+/',$txtContent);
foreach ($dsName as $val){
echo $val."
";
}
输入文件为:
,下面是提取出中文之后的输出文件内容:
,符合预期需求。

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

AI Hentai Generator
Generate AI Hentai for free.

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



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
