在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;
}
相關文檔:網頁中常用HTML字符實體
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com