PHP Chinese character substitution and pattern matching problem!! Please, everyone must see! _php Tutorials

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"!

http://www.bkjia.com/PHPjc/629376.html www.bkjia.com true http://www.bkjia.com/PHPjc/629376.html techarticle Author: Bluedoor Address: http://www.anbbs.com/anbbs/index.php?f_id=3page=1 This two days is doing a keyword highlighting the program, write good program in the local test also run well ...

  • 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.