The problem of Chinese character substitution and pattern matching in PHP please see _php tutorial

Source: Internet
Author: User
Author: Bluedoor
Original address: http://www.anbbs.com/anbbs/index.php?f_id=3&page=1
These two days are doing a keyword highlighting the program, write a good program in the local test also run well, but a page on a pile of garbage, don't say add light, it is no look!
I find the wrong, find out, find English no problem, encountered Chinese characters prone to problems, sometimes encountered Chinese characters will be a problem.
To summarize:
When using pattern matching, such as: Preg_match_all ($pat,......) With Preg_replace ($pat,......) ......
Things that are prone to problems are as follows:
Preg_match_all ("/(Kanji) +/ism", "I am a kanji, see what you do to me!" ", $m _a);
This pattern is simple enough to match the "kanji". This pattern contains Chinese characters that can be successfully matched, but do not be happy too early, the results are uncertain, why not sure you slowly look down.
The problem will occur as follows:
Preg_match_all ("/[kanji]+/ism", "I am a kanji, see what you do to me!" ", $m _a);
I want to match the appearance of "Han", "character" or "Chinese character". This must be a problem, match the result of a large group of garbled characters, may also have a death cycle. Why does this happen? Because PHP internal use is not Unicode, does not support multi-byte text, so a "Chinese character" is considered as 4bytes ASCII to do pattern matching, no error is strange!
Later I tried to re-write the pattern match and found a kind of seeming (why does it seem?). Look back) method can be solved:
Preg_match_all ("/(Han | word) +/ism", "I am a kanji, see what you do to me!" ", $m _a);
This can be matched with "Han", "word" or "kanji", $m the results in _a
Array
(
[0] = = Array
(
[0] = = Kanji

)
[1] = = Array
(
[0] = Word
)
)
How about a full-match string appears! But happy too early, and later in practice or will often out of question! To find the problem, finally found the root of the problem! PHP does not support multibyte text, so when it comes to pattern matching and character manipulation, the code is converted (I don't know if that's right), for example:
Eregi_replace ("Sex", "no", "sense of responsibility"), this operation is to replace the string "with responsibility" in the "sex" word "no", the final result is what? Because there is no "sex" in the "sense of responsibility" in a word, the result should be no replacement operation returned to the "responsible", but the result is "with a sense of the"!
Didn't think of it! Why? Look at the ASCII code. 2 ASCII codes A Chinese character "responsible" ASCII encoding is: 211,208 (yes), 212,240 (responsibility), 200,206 (ren), 184,208 (sense)
and the "Sex" code is: 208,212 (Sex), exactly with the 2nd byte and the 1th byte combination of the responsibility is consistent! So PHP will know to find the same pattern to match, split into half of the Chinese characters and replace the string after the combination, so the error!
At that time I thought the most common str_replace (), should not have the problem, but in fact Str_replace () performs the same operation also will be wrong! Now I think the substitution of Chinese characters was so lucky! May be at that time the substitution of Chinese characters are relatively long string of Chinese characters, it is not easy to appear above the situation. Even if there is no problem, you must know that it is not safe!
The problem is that work has to be done, and the difficulty of overcoming it is::: Now self.
Fortunately, a set of PHP extensions, multibyte String Functions, add many functions that support multibyte-text operations, such as: Ereg_replace () corresponds to Mb_ereg_replace () and so on. Please consult the relevant article for the specific function description.
Summary: It is best to use multibyte String Functions for the safe operation of Chinese characters.

http://www.bkjia.com/PHPjc/532003.html www.bkjia.com true http://www.bkjia.com/PHPjc/532003.html techarticle Author: Bluedoor Original address: http://www.anbbs.com/anbbs/index.php?f_id=3quot;/(kanji) +/ism, I am a Chinese character, see you how I do! , $m _a); This pattern is very simple to match the "Han ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.