javascript 與 php 對傳資料

之前都是使用 UTF-8編碼,可是這次卻碰到 big-5傳值的問題,幸好拜了google大神之後得到解答

  1. javascript 傳值 要使用 escape(str)

  2. php 則比較麻煩一點,還好有找到function可用

function uniDecode($str,$charcode){
$text = pregreplacecallback(“/%u[0-9A-Za-z]{4}/”,toUtf8,$str);
return mbconvertencoding($text, $charcode, ‘utf-8’);
}
function toUtf8($ar){
foreach($ar as $val){
$val = intval(substr($val,2),16);
if($val $c .= chr($val);
}elseif($val $c .= chr(0xC0 | ($val / 64));
$c .= chr(0x80 | ($val % 64));
}else{ // 0800-FFFF
$c .= chr(0xE0 | (($val / 64) / 64));
$c .= chr(0x80 | (($val / 64) % 64));
$c .= chr(0x80 | ($val % 64));
}
}
return $c;
}

除此之外還要使用

echo $string=iconv(“big5″,”UTF-8”,$string);

把資料echo出來

CaesarChi

Web developer, focus on website fullstack, special JavaScript, and love sharing developing experience and communicate with developers. http://about.me/clonn