<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,每个字符都会被当成一个字符而不是拆分成字节数据,所以不会产生这个问题

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

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.


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

SublimeText3 Linux new version
SublimeText3 Linux latest version

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

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.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function