简单快捷   随心所欲   企业建站首选YulinCMS

HTML 实体与网页编码
时间:2009年11月25日
来源:YulinCMS.com
阅读次数:
汉字都转化为了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');

 

JS 版

<script>
    test = "你好abc"
    str = ""
    for( i=0;   i<test.length; i++ )
    {
     temp = test.charCodeAt(i).toString(16);
     str   += "\\u"+ new Array(5-String(temp).length).join("0") +temp;
    }
    document.write (str)
</script>


vbs版

Function Unicode(str1)
    Dim str,temp
    str = ""
    For i=1   to len(str1)
     temp = Hex(AscW(Mid(str1,i,1)))
     If len(temp) < 5 Then   temp = right("0000" & temp, 4)
     str = str & "\u" & temp
    Next
    Unicode = str
End Function

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



coldfusion版

function nochaoscode(str)
{
    var new_str = “”;
    for(i=1; i lte len(str);i=i+1){
        if(asc(mid(str,i,1)) lt 128){
            new_str = new_str & mid(str,i,1);
        }else{
            new_str = new_str & “&##” & asc(mid(str,i,1));
        }
    }
    return new_str;
}
 



Powered by YulinCMS