一、编码范围
1. GBK (GB2312/GB18030)
\x00-\xff GBK双字节编码范围
\x20-\x7f ASCII
\xa1-\xff 中文
\x80-\xff 中文
2. UTF-8 (Unicode)
\u4e00-\u9fa5 (中文)
\x3130-\x318F (韩文
\xAC00-\xD7A3 (韩文)
\u0800-\u4e00 (日文)
ps: 韩文是大于[\u9fa5]的字符
正则例子:
preg_replace("/([\x80-\xff])/","",$str);
preg_replace("/([u4e00-u9fa5])/","",$str);
二、代码例子

Code
1
//判断内容里有没有中文-GBK (PHP)
2
function check_is_chinese($s){
3
return preg_match('/[\x80-\xff]./', $s);
4
}
5
6
//获取字符串长度-GBK (PHP)
7
function gb_strlen($str){
8
$count = 0;
9
for($i=0; $i<strlen($str); $i++){
10
$s = substr($str, $i, 1);
11
if (preg_match("/[\x80-\xff]/", $s)) ++$i;
12
++$count;
13
}
14
return $count;
15
}
16
17
//截取字符串字串-GBK (PHP)
18
function gb_substr($str, $len){
19
$count = 0;
20
for($i=0; $i<strlen($str); $i++){
21
if($count == $len) break;
22
if(preg_match("/[\x80-\xff]/", substr($str, $i, 1))) ++$i;
23
++$count;
24
}
25
return substr($str, 0, $i);
26
}
27
28
//统计字符串长度-UTF8 (PHP)
29
function utf8_strlen($str) {
30
$count = 0;
31
for($i = 0; $i < strlen($str); $i++){
32
$value = ord($str[$i]);
33
if($value > 127) {
34
$count++;
35
if($value >= 192 && $value <= 223) $i++;
36
elseif($value >= 224 && $value <= 239) $i = $i + 2;
37
elseif($value >= 240 && $value <= 247) $i = $i + 3;
38
else die('Not a UTF-8 compatible string');
39
}
40
$count++;
41
}
42
return $count;
43
}
44
45
46
//截取字符串-UTF8(PHP)
47
function utf8_substr($str,$position,$length){
48
$start_position = strlen($str);
49
$start_byte = 0;
50
$end_position = strlen($str);
51
$count = 0;
52
for($i = 0; $i < strlen($str); $i++){
53
if($count >= $position && $start_position > $i){
54
$start_position = $i;
55
$start_byte = $count;
56
}
57
if(($count-$start_byte)>=$length) {
58
$end_position = $i;
59
break;
60
}
61
$value = ord($str[$i]);
62
if($value > 127){
63
$count++;
64
if($value >= 192 && $value <= 223) $i++;
65
elseif($value >= 224 && $value <= 239) $i = $i + 2;
66
elseif($value >= 240 && $value <= 247) $i = $i + 3;
67
else die('Not a UTF-8 compatible string');
68
}
69
$count++;
70
71
}
72
return(substr($str,$start_position,$end_position-$start_position));
73
}
74
75
76
//字符串长度统计-UTF8 [中文3个字节,俄文、韩文占2个字节,字母占1个字节] (Ruby)
77
def utf8_string_length(str)
78
temp = CGI::unescape(str)
79
i = 0;
80
j = 0;
81
temp.length.times{|t|
82
if temp[t] < 127
83
i += 1
84
elseif temp[t] >= 127 and temp[t] < 224
85
j += 1
86
if 0 == (j % 2)
87
i += 2
88
j = 0
89
end
90
else
91
j += 1
92
if 0 == (j % 3)
93
i +=2
94
j = 0
95
end
96
end
97
}
98
return i
99
}
100
101
//判断是否是有韩文-UTF-8 (JavaScript)
102
function checkKoreaChar(str) {
103
for(i=0; i<str.length; i++) {
104
if(((str.charCodeAt(i) > 0x3130 && str.charCodeAt(i) < 0x318F) || (str.charCodeAt(i) >= 0xAC00 && str.charCodeAt(i) <= 0xD7A3))) {
105
return true;
106
}
107
}
108
return false;
109
}
110
//判断是否有中文字符-GBK (JavaScript)
111
function check_chinese_char(s){
112
return (s.length != s.replace(/[^\x00-\xff]/g,"**").length);
三、参考文档
http://www.unicode.org/
http://examples.oreilly.com/cjkvinfo/doc/cjk.inf
http://www.ansell-uebersetzungen.com/gbuni.html
http://www.haiyan.com/steelk/navigator/ref/gbk/gbindex.htm
http://baike.baidu.com/view/40801.htm
http://www.chedong.com/tech/hello_unicode.html
另:

Code
1
/*
2
* 中文截取,支持gb2312,gbk,utf-8,big5
3
*
4
* @param string $str 要截取的字串
5
* @param int $start 截取起始位置
6
* @param int $length 截取长度
7
* @param string $charset utf-8|gb2312|gbk|big5 编码
8
* @param $suffix 是否加尾缀
9
*/
10
public function csubstr($str, $start=0, $length, $charset="utf-8", $suffix=true)
11
{
12
if(function_exists("mb_substr"))
13
return mb_substr($str, $start, $length, $charset);
14
$re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
15
$re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
16
$re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
17
$re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
18
preg_match_all($re[$charset], $str, $match);
19
$slice = join("",array_slice($match[0], $start, $length));
20
if($suffix) return $slice."…";
21
return $slice;
22
}
23
24
posted on 2008-04-10 11:12
冷风旋 阅读(31)
评论(0) 编辑 收藏 网摘 所属分类:
PHP