欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

ajax如何传中文参数乱码解决办法

程序员文章站 2023-12-23 18:28:33
...
ajax如何传中文参数乱码解决办法 由于某种原因ajax默认就是uft-8的编码,那么我们使用一gbk就容易出现乱码的问题,下面我们就来告诉你php ajax乱码的解决方法吧。

ajax如何传中文参数乱码解决办法
由于某种原因ajax默认就是uft-8的编码,那么我们使用一gbk就容易出现乱码的问题,下面我们就来告诉你php教程 ajax乱码的解决方法吧。
传递中文参数,然后修改数据库教程的。


参数是中文encodeuricomponent这个方法必须调用两次
*/

function utf8rawurldecode ($source) {
$decodedstr = "";
$pos = 0;
$len = strlen ($source);
while ($pos $charat = substr ($source, $pos, 1);
if ($charat == '%') {
$pos++;
$charat = substr ($source, $pos, 1);
if ($charat == 'u') {
// we got a unicode character
$pos++;
$unicodehexval = substr ($source, $pos, 4);
$unicode = hexdec ($unicodehexval);
$entity = "". $unicode . ';';
$decodedstr .= utf8_encode ($entity);
$pos += 4;
}
else {
// we have an escaped ascii character
$hexval = substr ($source, $pos, 2);
$decodedstr .= chr (hexdec ($hexval));
$pos += 2;
}
} else {
$decodedstr .= $charat;
$pos++;
}
}
return $decodedstr;
}


/*
注:js中,在使用字符转码的时候,推荐使用 encodeuricomponent() 或者 encodeuri(),而非escape()。原因在于,escape() 只是为 ascii字符 做转换工作,转换成的 %unnnn 这样的码,如果要用更多的字符如 utf-8字符库就一定要用 encodeuricomponent() 或 encodeuri() 转换才可以成 %nn%nn 这的码才可以。

js:encodeuricomponent —— decodeuricomponent;php: rawurlencode —— rawurldecode


*/

上一篇:

下一篇: