该用户从未签到
|
AJAX传递中文字符串时必须把中文字符串编码成unicode,一般会用到JS的自带函数escape().不过找到了更好的函数来确决中文字符转换成unicode编码的函数! v+ N2 u/ `9 I) x- Y" Y# C
7 g& x a0 m s3 `: V3 P, B4 m! yfunction uniencode(text) $ t/ ~; }3 n( x. y O* h; ]0 f
{ ( F! {4 T; d) k: x& g% t: ^
text = escape(text.toString()).replace(/\+/g, "%2B");
8 ]% x/ X3 `( m, A var matches = text.match(/(%([0-9A-F]{2}))/gi);
4 _4 _8 J/ C, |) c if (matches)
$ e, G0 ]8 ^2 N/ u f" M7 }+ y { ) _8 H0 i2 s9 I9 D2 ?
for (var matchid = 0; matchid < matches.length; matchid++) ( }% F4 E# u% C8 r( {) H& U+ X- L, U
{
9 H, K/ d( j6 A var code = matches[matchid].substring(1,3); 3 Y* s9 y# |" d( V& j
if (parseInt(code, 16) >= 128) . R3 n7 H) Y- W" ` i
{
9 B, y4 G e0 V, |7 f( M text = text.replace(matches[matchid], '%u00' + code); $ ?% S6 P; [0 H5 ?
}
2 J1 ~2 g$ l7 U6 W( `* ^% A }
. l8 w+ B$ [! M, X } $ n& Z3 l" t- a7 e! p2 {
text = text.replace('%25', '%u0025');
# m: q* x3 D3 l9 C5 m) b/ ?* ?
! A4 U- p% H& k4 N return text; # t* o- m, c: s% A" r, L
}
8 b( b; L' X; m$ t- t8 Q$ U. p
. D3 L% g8 ?3 V5 N+ S1 Y7 [0 H5 z当然服务器端要对编码过的字符串进行第二次转码.把字符串转换成UTF-8编码.6 R7 c: G. q. P0 a/ Z$ I
3 d2 O$ ]! _9 d* \% I" j" }function convert_int_to_utf8($intval)
`+ P7 A' i! D$ A{ + |& F& U! G- Q& ~8 J3 s
$intvalintval = intval($intval); . b8 `2 h+ J: ]" Y, P
switch ($intval) " G4 p1 p/ l5 o- \
{
, Q% [" [# f' r+ z5 J* V // 1 byte, 7 bits
! S; o4 W$ _5 |" F' ~/ E6 Y case 0: ; P. M3 w; ?7 ~0 j4 M
return chr(0); ( A0 L4 F! A: D+ t7 i2 j+ i
case ($intval & 0x7F):
5 p7 ~+ }8 j" X: B4 M% X9 X2 C return chr($intval);
, P1 _! y7 z* Z( ]
5 y$ f2 L) H) c. t // 2 bytes, 11 bits
3 s+ ~7 R0 O: `1 \# H1 ] } case ($intval & 0x7FF):
R! f- A A& |4 Y1 b" d return chr(0xC0 | (($intval >> 6) & 0x1F)) . - t6 \. ~4 j. E0 I" {( _5 c
chr(0x80 | ($intval & 0x3F)); 1 j/ F& j2 Q! b0 H8 I4 y+ \& q- f
2 a# p5 L% {3 G3 U' u
// 3 bytes, 16 bits
" E* B7 c7 b' }+ i case ($intval & 0xFFFF): / }% S% O4 O( P& V5 ~: G! n4 Q2 D
return chr(0xE0 | (($intval >> 12) & 0x0F)) . / ^. ~8 U; |4 m
chr(0x80 | (($intval >> 6) & 0x3F)) .
% O' F* C* Q: H! ~+ h! e: [6 } chr (0x80 | ($intval & 0x3F));
$ g: q; F0 Z4 W; H2 N: H# s 5 l+ |# V* f. o. B9 }5 s$ y0 `3 Y
// 4 bytes, 21 bits
% U3 P) T" N7 U6 H! u2 f7 g1 Q case ($intval & 0x1FFFFF):
6 [, c, c K- y: f+ R& x return chr(0xF0 | ($intval >> 18)) .
/ y5 h4 K7 t$ s( F9 U/ ` chr(0x80 | (($intval >> 12) & 0x3F)) .
2 w. O3 g, N% @# r4 A" r1 V chr(0x80 | (($intval >> 6) & 0x3F)) .
0 ^( M! i. ~/ \) Q3 I. l4 s' v$ S' L chr(0x80 | ($intval & 0x3F)); 8 B H& C/ V- W$ I* `! q6 b
} - ~1 \3 T9 K& t- N
}
" v+ w, \' W8 u3 f |
|