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;
?>
转自:http://hi.baidu.com/loveyoursmile/blog
第一种方法,使用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;
?>
转自:http://hi.baidu.com/loveyoursmile/blog
本文介绍了在不同编码标准(UTF-8与GB2312)下网页内容的转换方法,通过JavaScript与PHP实现字符串的正确显示。具体探讨了使用encodeURIComponent进行一或二次转码的实践,并详细展示了如何在PHP中解析这些转码后的字符串。
836

被折叠的 条评论
为什么被折叠?



