<code><?php echo '<h2>正则表达式匹配中文<br>'; $a = '天地不仁,以万物为刍狗'; $b = preg_replace('/万/','萬',$a); echo $b; echo '<h2 id="加上方括号后替换结果出现乱码">加上方括号后替换结果出现乱码</h2> <br>'; $c = '天地不仁,以万物为刍狗'; $d = preg_replace('/[万]/','萬',$a); echo $d; ?> </code>
以上程序运行结果可以在http://nyaii.com/s/test.php看到。不知为何,为匹配的中文字符加上方括号后就出现了乱码。同样的情形,在javascript中执行就一切正常。
<code> '天地不仁'.replace(/[天]/,'') //outputs "地不仁" </code>
回复内容:
<code><?php echo '<h2>正则表达式匹配中文<br>'; $a = '天地不仁,以万物为刍狗'; $b = preg_replace('/万/','萬',$a); echo $b; echo '<h2 id="加上方括号后替换结果出现乱码">加上方括号后替换结果出现乱码</h2> <br>'; $c = '天地不仁,以万物为刍狗'; $d = preg_replace('/[万]/','萬',$a); echo $d; ?> </code>
以上程序运行结果可以在http://nyaii.com/s/test.php看到。不知为何,为匹配的中文字符加上方括号后就出现了乱码。同样的情形,在javascript中执行就一切正常。
<code> '天地不仁'.replace(/[天]/,'') //outputs "地不仁" </code>
加上UTF8修饰符即可
<code>$d = preg_replace('/[万]/u','萬',$a);</code>
其余修饰符请见
http://php.net/manual/en/reference.pcre.pattern.modifiers.php
以下为对于题主评论中的问题的补充内容
关于为什么[]内就需要加u修饰符的问题,实际上严格来说,两种场合你最好都加上u修饰符
但为什么[]就会导致乱码呢,这就要从字节层面而不是字符层面来解释了。
首先我们知道PHP的字符串并不是Unicode进行存储的,然后我们来看下这个代码
<code><?php $a = "万"; echo strlen($a); //3 for ($i = 0; $i < strlen($a); $i++) { echo dechex(ord($a[$i])) . ' '; //e4 b8 87 }</code></code>
我们可以拿到"万"字的utf8十六进制编码是e4b887
所以在没有开启utf8修饰符的时候,正则表达式引擎并没有把"万"当成一个独立的字符,而是三个字节的连续数据。
以下是结论:
当没有[]进行匹配的时候,它寻找的是十六进制编码值为 e4 b8 87 的三个连续字符,换句话说,实际上你的模式是
\xe4\xb8\x87
,但这种连续字符的出现在你的字符串中,只有"万"字能对上,所以替换了并不会有乱码。但如果你的字符串里面可能还要包括四字节的utf8编码字符,例如emoji,可能就会导致问题了当你在万外面包装了[],正则表达式引擎实际上找的是
[\xe4\xb8\x87]
,懂正则表达式的很快就能发现它实际上是匹配这三个字符的任意一个,所以这个时候就会影响到除了万以外的别的汉字了当你加了utf8修饰符之后,"万"会被正则表达式当成是一个独立的字符,所以不再会产生这个问题
至于javascript,因为它对字符编码是原生的unicode,每个字符都会被当成一个字符而不是拆分成字节数据,所以不会产生这个问题

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.

In PHP, use the clone keyword to create a copy of the object and customize the cloning behavior through the \_\_clone magic method. 1. Use the clone keyword to make a shallow copy, cloning the object's properties but not the object's properties. 2. The \_\_clone method can deeply copy nested objects to avoid shallow copying problems. 3. Pay attention to avoid circular references and performance problems in cloning, and optimize cloning operations to improve efficiency.

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.


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

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

WebStorm Mac version
Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download
The most popular open source editor