当前第2页 返回上一页
AJAX使用GET请求时传递中文字符串时也必须把中文字符串编码成unicode,一般会用到JS的自带函数escape().不过找到了更好的函数来确决中文字符转换成unicode编码的函数如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | function uniencode(text)
{
text = escape(text.toString()).replace(/+/g, "%2B" );
var matches = text.match(/(%([0-9A-F]{2}))/gi);
if (matches)
{
for ( var matchid = 0; matchid < matches.length; matchid++)
{
var code = matches[matchid].substring(1,3);
if (parseInt(code, 16) >= 128)
{
text = text.replace(matches[matchid], '%u00' + code);
}
}
}
text = text.replace( '%25' , '%u0025' );
return text;
}
|
当然服务器端要对编码过的字符串进行第二次转码.把字符串转换成UTF-8编码.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | function convert_int_to_utf8( $intval )
{
$intval = intval ( $intval );
switch ( $intval )
{
case 0:
return chr (0);
case ( $intval & 0x7F):
return chr ( $intval );
case ( $intval & 0x7FF):
return chr (0xC0 | (( $intval >> 6) & 0x1F)) .
chr (0x80 | ( $intval & 0x3F));
case ( $intval & 0xFFFF):
return chr (0xE0 | (( $intval >> 12) & 0x0F)) .
chr (0x80 | (( $intval >> 6) & 0x3F)) .
chr (0x80 | ( $intval & 0x3F));
case ( $intval & 0x1FFFFF):
return chr (0xF0 | ( $intval >> 18)) .
chr (0x80 | (( $intval >> 12) & 0x3F)) .
chr (0x80 | (( $intval >> 6) & 0x3F)) .
chr (0x80 | ( $intval & 0x3F));
}
}
|
【推荐学习:javascript高级教程】
以上就是javascript字符串如何进行编码转换的详细内容,更多文章请关注木庄网络博客!
返回前面的内容
相关阅读 >>
javascript 中的 nan
哪些游戏是用javascript制作的?
javascript怎么改变css
javascript怎么把字符转数组
对javascript开发者非常有用的10个奇淫巧计
javascript字符串怎么替换字符
通过代码示例,了解css3+javascript按钮水波纹效果
javascript和ajax的区别是什么
javascript中的var和let的区别(代码示例)
javascript中创建对象的7种经典方式(总结)
更多相关阅读请进入《javascript》频道 >>
人民邮电出版社
本书对 Vue.js 3 技术细节的分析非常可靠,对于需要深入理解 Vue.js 3 的用户会有很大的帮助。——尤雨溪,Vue.js作者
转载请注明出处:木庄网络博客 » javascript字符串如何进行编码转换