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");

?>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章