查看文章
 
javascript+php中文url编码|gb2312问题(ajax中文参数)
2009-10-09 16:12
php页面文档为utf-8编码,html页面为gb2312。

第一种方法,使用encodeURIComponent一次转码:
"你好,world!"在html页面得出的编码为"%E4%BD%A0%E5%A5%BD%EF%BC%8Cworld!":
<html>
<head>
<script language="javascript">
a = encodeURIComponent('你好,world!');
window.location.href = a;
alert(a);
</script>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<head>
<body>
你好
</body>
</html>

php页面转码,得出中文"你好,world!"(utf-8编码):
<?php
header("Content-type:text/html; charset=utf-8");
$string = '%E4%BD%A0%E5%A5%BD%EF%BC%8Cworld!';
$string = urldecode($string);
echo $string;
?>


第二种方法,使用encodeURIComponent两次转码:
"你好,world!"在html页面得出的编码为"%25E4%25BD%25A0%25E5%25A5%25BD%25EF%25BC%258Cworld!":
<html>
<head>
<script language="javascript">
a = encodeURIComponent(encodeURIComponent('你好,world!'));
window.location.href = a;
alert(a);
</script>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<head>
<body>
你好
</body>
</html>

php页面转码,得出中文"你好,world!"(utf-8编码):
<?php
header("Content-type:text/html; charset=utf-8");
$string = '%25E4%25BD%25A0%25E5%25A5%25BD%25EF%25BC%258Cworld!';
$string = urldecode($string);
$string = iconv("UTF-8","GB2312",$string);
$string = urldecode($string);
echo $string;
?>

可参考:
php在GB2312页面下提交Ajax编码转换的解决方法

类别:Php Manual||添加到搜藏 |分享到i贴吧|浏览(5665)|评论 (0)
 
 
最近读者:
 
网友评论:
发表评论:
姓 名:
网址或邮箱: (选填)
内 容:
     

   
帮助中心 | 空间客服 | 投诉中心 | 空间协议
©2012 Baidu