Tip: PHP titles about Chinese character exchange and pattern matching

Source: Internet
Author: User
In the past two days, I am working on a program to highlight key words. The program I have written has been well tested locally, but a pile of garbled code is displayed on the page, let alone highlight it. you just don't have to watch it! I am looking for an error.

In the past two days, I am working on a program to highlight key words. The program I have written has been well tested locally, but a pile of garbled code is displayed on the page, let alone highlight it. you just don't have to watch it!

I am looking for a mistake. I am looking for it. I have no English title for the invention. when I encounter a Chinese character, it is easy to give a title. sometimes, when I encounter a Chinese character, I have to give a title.

Summary:

For example, preg_match_all ($ pat ,......) And preg_replace ($ pat ,......)......

The title is easily displayed as follows:
Preg_match_all ('/(Chinese character)/ism', 'I am a Chinese character, see what you think of me! ', $ M_a );
This pattern is simply to match the Chinese character ". In this mode, Chinese characters can be matched successfully, but you should not be too happy. The results are constantly determined.

The title must be displayed as follows:
Preg_match_all ('/[Chinese characters]/ism', 'I am a Chinese character, see what you think of me! ', $ M_a );
We wanted to match and present "Chinese", "word", or "Chinese character ". This will display the title, and the matching results will be garbled, and there may be a passing loop. Why does this happen? Because the internal application of PHP is not UNICODE and does not support multi-byte text, a 'Chinese character 'is regarded as 4 bytes ASCII for pattern matching. it is strange that there is no error!

Later, I tried to re-write the pattern match and invented one (why? Later) the solution can be:
Preg_match_all ('/(Chinese | word)/ism', 'I am a Chinese character, see what you think of me! ', $ M_a );

In this way, we can match the results in "Chinese", "word", or "Chinese character", $ m_a.

Array
(
[0] => Array
(
[0] => Chinese characters
)

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

)

How to render a fully matched string! However, I was so happy that I often came up with titles in actual use! Search for the title and find the root of the title! PHP does not support multi-byte text, so the internal code is converted into backward lines during pattern matching and character control (I don't know if this is correct). For example:

Eregi_replace ', what is the final result? Because there is no sense of obligation in the word "gender", the result should be that there is no fulfillment of the exchange control to return a "sense of obligation", but the result is actually a "sense of uncertainty '!

I did not expect it! Why? Take a look at the ASCII code and you will understand it. the Two ASCII codes: 211,208 (with), 212,240 (with responsibility), and the ASCII codes of a Chinese character with a sense of obligation ), 200,206 (ren), 184,208 (sense)

The encoding of sex is 208,212, which is exactly the same as the combination of 2nd bytes and 1st bytes! So PHP is familiar with finding the same pattern for matching, splitting half of the Chinese characters and then combining them with the exchanged strings, so there is an error!

At that time, I thought the most commonly used str_replace () should not have a title, but in fact, str_replace () will also fail to perform the same hold! Now I think it is a great pleasure to exchange Chinese characters in the past! It may be that the Chinese character exchange at that time was a long string of Chinese characters, which was not easy to present. Even if there is no title, you must know that it is not safe!

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.