HTML实体与网页编码(汉字转化为了html实体)

汉字都转化为了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<str.length; 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;
 }


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值