该用户从未签到
|
AJAX传递中文字符串时必须把中文字符串编码成unicode,一般会用到JS的自带函数escape().不过找到了更好的函数来确决中文字符转换成unicode编码的函数9 N) t0 ^6 q5 u3 h7 y9 s
! L" N$ R p: \! r. cfunction uniencode(text) / L+ d" m; q4 l0 w& n( {) x
{ ) u8 d4 o2 b2 X" L2 {& K& X" N5 z
text = escape(text.toString()).replace(/\+/g, "%2B"); ! `. w6 H4 o- s; R
var matches = text.match(/(%([0-9A-F]{2}))/gi);
" Q9 E k, q" S$ m* K; S if (matches) 5 X" `' j% ]: D _! s
{
" |6 U5 m- L9 ] for (var matchid = 0; matchid < matches.length; matchid++) 0 q. r. D& M( K5 u9 l: v
{
, P& f& G2 N/ C5 L' G# h, ~ V var code = matches[matchid].substring(1,3);
: ]% V. E* E# A if (parseInt(code, 16) >= 128) & |1 |5 e$ M, v5 H2 B& @8 o3 A
{
5 d- F4 v( p8 U. f text = text.replace(matches[matchid], '%u00' + code); # P2 N; x, q2 {( W/ |
}
2 w2 o) a/ I F) A) h* M. d }
) c, X. B7 y4 [* t } 7 V, ?( s$ n( w7 N3 U1 e L* T9 b5 D
text = text.replace('%25', '%u0025');
: T$ Z2 n' x+ {& t L 9 l2 {: C( D, }
return text; # V8 d* l+ a# C4 E
} 8 u; S" x" Q- d$ l$ V( }; g
3 E F# n1 e; R4 K当然服务器端要对编码过的字符串进行第二次转码.把字符串转换成UTF-8编码.0 g: t6 j, t- X: o
: a. f" J" Y+ U$ D/ p! h
function convert_int_to_utf8($intval)
/ S7 S. P+ \, |; H i* a% w2 ]6 ?{ ) E& ~* f+ }7 t7 m5 l- ~! f+ |- O) ^, F
$intvalintval = intval($intval);
3 Q9 t3 L& r; P" |: v! k switch ($intval)
U5 M" \7 X) r+ ? { " ^& k0 d% |1 i3 C C, z
// 1 byte, 7 bits
( R( Y. a! b/ d9 v( ?* s case 0:
6 V) E0 x8 d5 [: [ return chr(0); 0 T4 k, o; J* m: {" ~: T
case ($intval & 0x7F):
. o: |# _/ R; @! W! d+ [ return chr($intval);
4 h$ A6 j+ N# G6 K0 w: s% l( G9 I R
2 V" R) j4 R. [% s // 2 bytes, 11 bits
. i2 C9 P/ c) G case ($intval & 0x7FF): ' Z1 l& I8 n1 u0 k% V5 y8 o
return chr(0xC0 | (($intval >> 6) & 0x1F)) .
: L( q; q" l+ B/ J chr(0x80 | ($intval & 0x3F)); - ], ~% _- K$ Z& l d+ l3 v9 c
2 @: p# ]6 P: i6 D // 3 bytes, 16 bits
( f; N$ [8 U) S1 \" x4 o case ($intval & 0xFFFF): 0 r P# J2 y" X) B8 a& m/ n: I
return chr(0xE0 | (($intval >> 12) & 0x0F)) . / J3 M1 Z. P$ K; s+ d
chr(0x80 | (($intval >> 6) & 0x3F)) .
8 ~5 b4 C$ z2 E) x- N3 f chr (0x80 | ($intval & 0x3F)); ' N# M# R9 R; p2 U. a4 }, P
6 s! Z( X3 v4 {" t0 I
// 4 bytes, 21 bits 1 `: b% D: X) c, w, i# r2 ]0 M
case ($intval & 0x1FFFFF): 4 j7 [" x ?, O/ ^3 G1 I6 c5 [+ f
return chr(0xF0 | ($intval >> 18)) .
+ n* {% p& Y3 `" { chr(0x80 | (($intval >> 12) & 0x3F)) .
, s; e- [( L: `0 P chr(0x80 | (($intval >> 6) & 0x3F)) . 3 m' c* Y& U6 A$ z2 \8 Q4 I z
chr(0x80 | ($intval & 0x3F));
/ E h# C* }7 a2 p) b }
& c8 F- k, }! @}
/ z( @* s& e# i9 H4 U, E4 p |
|