HTML實體與網頁編碼

巴扎黑
發布: 2017-04-05 11:46:02
原創
2177 人瀏覽過

漢字都轉換為了html實體(十進位表示的Unicode編碼),這樣做的好處就是不管網頁的編碼是什麼,都可以正常的顯示漢字,而不會出現亂碼,當然也適用於其他字元集。

在php中我們可以用mbstring的mb_convert_encoding函數來實現這個正向及反向的轉換。
如:

mb_convert_encoding ("你好", "HTML-ENTITIES", "gb2312");    //輸出:你好
mb_convert_encoding ("你好", "gb2312", "HTML-ENTITIES");    //輸出:你好 
#  

# 如果需要對整個頁面轉化,則只需要在php檔案的頭部加上這三行程式碼:
mb_internal_encoding("gb2312");  // 這裡的gb2312是你網站原來的編碼
mb_http_output("HTML-ENTITIES");
ob_start('mb_output_handler');
 

# Asp版 可以用下面這個函數來實現這個轉換:

Function htmlentities(str)
    For i = 1 to Len(str)
        char = mid(str, i, 1)
        If AscW(char) > 0 then
            htmlentities = htmlentities & "" & Ascw(char) & ";"
        Else
#             htmlentities = htmlentities & "" & (65536 + ascW(char)) & ";"
        End if
    Next
End Function 

JS 版

 function htmlentities(str)
 {
      var r = "";
      for( i=0; i       {
           temp = str.charCodeAt(i);
           r += ""+temp+";";
      }
#      


##      //  也可以用一句正規表示式解
     // r = str.replace(/[\d\D]/g, function($0) { return "" + $0.charCodeAt(0) + ";"; });
     return r;
 }
asp.net (c#) 版

 private string GetHtmlEntities(string str)
  {
      string r = string.Empty;
       for (int i = 0; i < str.Length; i++)
       {
            r += ""+Char.ConvertToUtf32(str,i)+";";
       }

       return r;

 }

### 相關文件:網頁中常用HTML字元實體###

以上是HTML實體與網頁編碼的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!