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; }
相关文档:网页中常用HTML字符实体
以上就是HTML实体与网页编码的详细内容,更多请关注Gxl网其它相关文章!