PHP Regular Expression matches Chinese and Japanese characters (GBK)

Source: Internet
Author: User
Tags php regular expression

The first is the encoding range of these non-English characters:

Here are the main non-English character ranges

2e80 ~ 33ffh: Symbol area of China, Japan, and South Korea. Reception of Kangxi Dictionary heads, China-Japan-South Korea auxiliary departments heads, phonetic symbols, Japanese Kana, Korean Notes, Chinese-Japan-South Korea symbols, punctuation marks, circled or including Rune numbers, months, and Japanese Kana combination, unit, year, month, date, and time.

3400 ~ 4 dffh: Japan and South Korea recognized the expansion of ideographic text area A, a total of 6,582 Chinese and Korean characters.

4e00 ~ 9 fffh: Japan and South Korea recognized the ideographic text area, a total of 20,902 Chinese and Korean characters.

A000 ~ A4ffh: Yi text area, which contains the texts and roots of Yi people in southern China.

Ac00 ~ D7ffh: A combination area of Korean and pinyin. It contains text in Korean Notes.

F900 ~ Faffh: compatible with ideographic text area, a total of 302 Chinese and Korean characters.

Fb00 ~ Fffdh: it is a text expression area that contains a combination of Latin characters, Hebrew characters, Arabic characters, Chinese-Japanese vertices, small characters, halfwidth characters, and fullwidth characters.

For example, to match all Chinese and Korean non-symbolic characters, the regular expression should be ^ [\ u3400-\ u9fff] + $
Theoretically, I copied a Korean file to MSN. co. Ko and found that it was not correct.
Copy A 'handler' to msn.co.jp ..

Then, expand the range to ^ [\ u2e80-\ u9fff] + $. This is all done. This should be the regular expression that matches the Chinese and Japanese characters, including traditional Chinese that we are still using blindly.

The regular expression for Chinese characters should be ^ [\ u4e00-\ u9fff] + $, which is very close to the ^ [\ u4e00-\ u9fa5] + $

Note that ^ [\ u4e00-\ u9fa5] + $ is a regular expression used to match simplified Chinese characters. In fact, traditional Chinese characters are also in the regular expression, I used the tester to test the 'central People's Republic of Korea 'and also passed the test. Of course, ^ [\ u4e00-\ u9fff] + $ is the same result.

 

Then, the above ^ [\ u4e00-\ u9fa5] + $ regular expressions are not supported by PHP. The following solutions can be found on stackoverflow:

PCRE does not support\ UxxxxSyntax. Use\ X {XXXX}Instead. See here.

Your\ U2e80-\ u9fffRange is also equivalent

\ P {incjk_radicals_supplement} \ P {inkangxi_radicals} \ P {region} \ P {inhiragana} \ P {inkatakana} \ P {inbopomofo} \ P {region} \ P {inkanbun} \ P {inbopomofo_extended} \ P {inkatakana_phonetic_extensions} \ P {region} \ P {incjk_compatibility} \ P {region}

Don't forget to addUModifier (/RegEx here/u) If you're dealing with UTF-8. If you're dealing with another multi-byte encoding, you must first convert it to UTF-8.

 

That is to say, PHP needs to write/[\ x {2e80}-\ x {9fff}] +/u like this.

 

In addition, during actual operations, we may find that using the preg_replace function produces results that do not match our expectation.

 
$ Str= 'Hello, this is selling coffee! ';

For example, we define the above string and then use the preg_replace function. This is the encoding problem. The matching string we use is UTF-8 encoded by default, but $ STR is probably GBK. At this time, we need to use the iconv function to encode and convert it first.

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.