Home php教程 php手册 php中文汉字替换与模式匹配的问题!!请大家必看!

php中文汉字替换与模式匹配的问题!!请大家必看!

Jun 21, 2016 am 09:15 AM
ascii match quot replace

汉字|问题|中文

作者: bluedoor
原帖地址:http://www.anbbs.com/anbbs/index.php?f_id=3&page=1
  这两天正在做一个关键字加亮显示的程序,写好的程序在本地测试也跑得好好的,可是一上去页面就出现一堆一堆的乱码,别说加亮了,简直就是没的看!

  我就找错误,找来找去,发现英文没有问题,遇到汉字容易出问题,有的时候遇到汉字必出问题。

  总结一下:

  当使用模式匹配的时候,如:preg_match_all($pat,……)与preg_replace($pat,……)……

  容易出问题的情况如下:
  preg_match_all("/(汉字)+/ism","我是汉字,看你把我怎么着!",$m_a);
  这个模式很简单就是匹配出“汉字”。这种情况模式中包含汉字可以成功匹配出来,但是也不要高兴得太早,结果不确定,为什么不确定你慢慢往下看。

  必出现问题情况如下:
  preg_match_all("/[汉字]+/ism","我是汉字,看你把我怎么着!",$m_a);
  本想匹配出现“汉”、“字”或者“汉字”。这个必出现问题,匹配的结果一大群乱码,没准还会出个死循环呢。为什么会出现这种情况?是因为PHP内部使用不是UNICODE,不支持多字节文字,所以一个"汉字"就被当成4bytes的ASCII去进行模式匹配,不出错才怪呢!

  后来我又试试重新写一下模式匹配,发现一种似乎(为什么说似乎?往后看)方法可以解决:
  preg_match_all("/(汉|字)+/ism","我是汉字,看你把我怎么着!",$m_a);

  这样写可以匹配出“汉”、“字”或者“汉字”,$m_a中的结果

Array
(
[0] => Array
(
[0] => 汉字
)

[1] => Array
(
[0] => 字
)

)

  怎么样全匹配的字符串出现了吧!可是高兴得太早了,后来在实际中用还是会经常出问题!再去找问题,终于找到问题的根了!PHP不支持多字节文字,所以在进行模式匹配与字符操作的时候都是内码转化后进行的(我不知道这样说对不对),举个实例吧:

  eregi_replace("性","没有" , "有责任感");这个操作就是要把字符串"有责任感"中"性"字替换成"没有",最后的结果是什么?因为"有责任感"中没有"性"就个字,结果应该是没有执行替换操作返回"有责任感",可是结果竟然是"用挥叙任感"!

  没想到吧!为什么?看一下ASCII码你就明白了,2个ASCII码代码一个汉字"有责任感"的ASCII编码依次为:211,208(有),212,240(责),200,206(任),184,208(感) 

  而"性"的编码为:208,212(性),恰好与有的第2字节和责的第1字节组合是一致的!所以PHP就认识找到相同的模式进行匹配,拆成一半的汉字再与替换后的字串进行组合,所以就出错了!

  当时我想最常用的str_replace(),应该不会有问题的,但是事实上str_replace()执行同样的操作也会出错!现在我想以前进行汉字替换实在是太幸运了!可能是那个时候进行的汉字替换都是比较长的汉字串吧,不太容易出现以上的情况。即使没有出问题,也要知道那是不安全的!

  问题是有的,工作还要继续做,克服的困难也就::::现在的自我了。

  好在想起一组PHP的扩展模块,Multibyte String Functions,添加许多支持多字节文字的操作的函数,如:ereg_replace() 对应着mb_ereg_replace() 等等。具体的函数说明请查询相关的文章。

  总结:对于中文汉字安全的操作最好是使用Multibyte String Functions。



Statement of this Website
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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How many bytes does one ascii character occupy? How many bytes does one ascii character occupy? Mar 09, 2023 pm 03:49 PM

One ascii character occupies 1 byte. ASCII code characters are represented by 7-bit or 8-bit binary encoding in the computer and are stored in one byte, that is, one ASCII code occupies one byte. ASCII code can be divided into standard ASCII code and extended ASCII code. Standard ASCII code is also called basic ASCII code. It uses 7-bit binary numbers (the remaining 1 binary digit is 0) to represent all uppercase and lowercase letters, and the numbers 0 to 9. Punctuation marks, and special control characters used in American English.

Quickly learn about ASCII value conversion in PHP Quickly learn about ASCII value conversion in PHP Mar 28, 2024 pm 06:42 PM

ASCII value conversion in PHP is a problem often encountered in programming. ASCII (American Standard Code for Information Interchange) is a standard encoding system for converting characters into numbers. In PHP, we often need to convert between characters and numbers through ASCII code. This article will introduce how to convert ASCII values ​​in PHP and give specific code examples. 1. Change the characters

How to use the REPLACE function to replace a specified part of a string in MySQL How to use the REPLACE function to replace a specified part of a string in MySQL Jul 25, 2023 pm 01:18 PM

MySQL is a commonly used relational database management system that provides a variety of functions to process and operate data. Among them, the REPLACE function is used to replace the specified part of the string. In this article, we will introduce how to use the REPLACE function for string replacement in MySQL and demonstrate its usage through code examples. First, let’s take a look at the syntax of the REPLACE function: REPLACE(str,search_str,replace_str).

What are the string search and replace techniques in Python? What are the string search and replace techniques in Python? Oct 20, 2023 am 11:42 AM

What are the string search and replace techniques in Python? (Specific code example) In Python, strings are a common data type, and we often encounter string search and replace operations in daily programming. This article will introduce some common string search and replacement techniques, accompanied by specific code examples. To find a specific substring in a string, you can use the find() method or index() method of the string. The find() method returns the index of the first occurrence of the substring in the string.

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

PHP returns the ASCII value of the first character of the string PHP returns the ASCII value of the first character of the string Mar 21, 2024 am 11:01 AM

This article will explain in detail the ASCII value of the first character of the string returned by PHP. The editor thinks it is very practical, so I share it with you as a reference. I hope you can gain something after reading this article. PHP returns the ASCII value of the first character of a string Introduction In PHP, getting the ASCII value of the first character of a string is a common operation that involves basic knowledge of string processing and character encoding. ASCII values ​​are used to represent the numeric value of characters in computer systems and are critical for character comparison, data transmission and storage. The process of getting the ASCII value of the first character of a string involves the following steps: Get String: Determine the string for which you want to get the ASCII value. It can be a variable or a string constant

What are the differences between unicode and ascii What are the differences between unicode and ascii Sep 06, 2023 am 11:56 AM

The differences between unicode and ascii include different encoding ranges, different storage spaces, and different compatibility. Detailed introduction: 1. The encoding range is different. The encoding range of ASCII is 0-127, which is mainly used to represent English letters. The encoding range of Unicode is much wider and can represent almost all language characters; 2. The storage space is different. ASCII usually Use 1 byte to store a character, while unicode may use 2 or more bytes to store a character; 3. Different compatibility, etc.

How to accurately convert PHP string to ASCII code How to accurately convert PHP string to ASCII code Mar 27, 2024 am 10:48 AM

"How to accurately convert PHP strings to ASCII codes, specific code examples are needed" In the field of programming, ASCII (American Standard Code for Information Interchange) code is the standard encoding system used to represent characters in computer systems. In PHP, we often need to convert strings into ASCII codes for some operations or processing. Here's how to accurately convert a string to ASCII in PHP

See all articles