---
title: "php 正则表达式 匹配中日韩字符（GBK）"
author: "kirenenko04"
date: "2014-12-08T13:56:49Z"
tags: []
categories: ["编程语言"]
slug: "php-gbk"
draft: false
lastmod: "2014-12-08T13:56:49Z"
id: 2164140
url: "https://kirenenko04.iteye.com/blog/2164140"
---

首先是这些非英文字符的编码范围：

这里是几个主要非英文语系字符范围

2E80～33FFh：中日韩符号区。收容康熙字典部首、中日韩辅助部首、注音符号、日本假名、韩文音符，中日韩的符号、标点、带圈或带括符文数字、月份，以及日本的假名组合、单位、年号、月份、日期、时间等。

3400～4DFFh：中日韩认同表意文字扩充A区，总计收容6,582个中日韩汉字。

4E00～9FFFh：中日韩认同表意文字区，总计收容20,902个中日韩汉字。

A000～A4FFh：彝族文字区，收容中国南方彝族文字和字根。

AC00～D7FFh：韩文拼音组合字区，收容以韩文音符拼成的文字。

F900～FAFFh：中日韩兼容表意文字区，总计收容302个中日韩汉字。

FB00～FFFDh：文字表现形式区，收容组合拉丁文字、希伯来文、阿拉伯文、中日韩直式标点、小符号、半角符号、全角符号等。

比如需要匹配所有中日韩非符号字符,那么正则表达式应该是^\[\\u3400-\\u9FFF]+$   
理论上没错, 可是我到msn.co.ko随便复制了个韩文下来, 发现根本不对, 诡异   
再到msn.co.jp复制了个'お', 也不得行..

然后把范围扩大到^\[\\u2E80-\\u9FFF]+$, 这样倒是都通过了, 这个应该就是匹配中日韩文字的正则表达式了, 包括我們臺灣省還在盲目使用的繁體中文

而关于中文的正则表达式, 应该是^\[\\u4E00-\\u9FFF]+$, 和论坛里常被人提起的^\[\\u4E00-\\u9FA5]+$很接近

需要注意的是论坛里说的^\[\\u4E00-\\u9FA5]+$这是专门用于匹配简体中文的正则表达式, 实际上繁体字也在里面, 我用测试器测试了下'中華人民共和國', 也通过了, 当然, ^\[\\u4E00-\\u9FFF]+$也是一样的结果

 

然后上面的^\[\\u4E00-\\u9FA5]+$形式的正则表达式php是不支持的。在stackoverflow上可以找到如下解决方案：

PCRE does not support the `\uXXXX` syntax. Use `\x{XXXX}` instead. See [here](http://www.regular-expressions.info/unicode.html).

Your `\u2e80-\u9fff` range is also equivalent to

`\p{InCJK_Radicals_Supplement}\p{InKangxi_Radicals}\p{InIdeographic_Description_Characters}\p{InCJK_Symbols_and_Punctuation}\p{InHiragana}\p{InKatakana}\p{InBopomofo}\p{InHangul_Compatibility_Jamo}\p{InKanbun}\p{InBopomofo_Extended}\p{InKatakana_Phonetic_Extensions}\p{InEnclosed_CJK_Letters_and_Months}\p{InCJK_Compatibility}\p{InCJK_Unified_Ideographs_Extension_A}\p{InYijing_Hexagram_Symbols}\p{InCJK_Unified_Ideographs}`

Don't forget to add the `u` modifier (`/regex here/u`) if you're dealing with UTF-8. If you're dealing with another multi-byte encoding, you must first [convert](http://php.net/mb_convert_encoding) it to UTF-8.

 

也就是说，php要这样写/\[\\x{2E80}-\\x{9FFF}]+/u