PHP读取xml乱码 | 编码转换 mb_convert_encoding

<?xml version="1.0" encoding="gb2312"?>
<msg>
<E600>未知错误</E600>
<E601>验证码输入错误</E601>
<E602>此用户名已被注册</E602>
<E603>抱歉,由于系统原因,账户注册失败</E603>
<E604>登录失败:用户名或密码错误</E604>
<E605>您要浏览的帖子不存在</E605>
<E606>您要浏览的贴吧不存在</E606>
<E607>您已经申请了会员</E607>
<E608>抱歉,收件人没有找到</E608>
<E609>对不起,您没有登录,无权限进行该操作</E609>
<E610>收件人列表中有重复</E610>
<E611>对不起,您没有权限进行该操作</E611>
</msg>


<?php
session_start();
$ErrorID=$_SESSION["iError"]; # 错误代码
if ($ErrorID==""){
$ErrorID="E600";
}

$xml=simplexml_load_file("Xml/Error.xml");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>错误</title>
<link href="CSS/Text.css" rel="stylesheet" type="text/css" />
</head>

<body>
<div>
  <table width="100%" border="0">
    <tr>
      <td align="center"><form id="form1" name="form1" method="post" action="">
        <table width="80%" border="1">
          <tr>
            <td height="21" align="center" bgcolor="#999999" class="Text1"><b>错误</b></td>
          </tr>
          <tr>
            <td height="140" align="center" class="Text1"><?php echo $xml->$ErrorID; ?></td>
          </tr>
        </table>
            </form>
      </td>
    </tr>
  </table>
</div>
</body>
</html>

为何会乱码

 

==================================================

 

乱码,一般都是编码不统一,在读取后;尽量进行编码转换:

用的函数是:mb_convert_encoding


1、把 GBK 编码字串转换成 UTF-8 编码字串

Php代码
<?php  
header("content-Type: text/html; charset=Utf-8");  
echo mb_convert_encoding("你是我的好朋友", "UTF-8", "GBK");  
?> 

<?php
header("content-Type: text/html; charset=Utf-8");
echo mb_convert_encoding("你是我的好朋友", "UTF-8", "GBK");
?>

2、把 UTF-8 编码字串转换成 GB2312 编码字串

Php代码
// 注意将此文件存盘成  utf-8 编码格式文件再测试  
<?php  
header("content-Type: text/html; charset=gb2312");  
echo mb_convert_encoding("你是我的好朋友", "gb312", "utf-8");  
?>  

// 注意将此文件存盘成  utf-8 编码格式文件再测试
<?php
header("content-Type: text/html; charset=gb2312");
echo mb_convert_encoding("你是我的好朋友", "gb312", "utf-8");

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值