Chinese characters are converted into HTML entities (Unicode encoding in decimal representation). The advantage of this is that no matter what the encoding of the web page is, Chinese characters can be displayed normally without garbled characters. Of course, it is also applicable to other character sets.
In PHP, we can use the mb_convert_encoding function of mbstring to achieve this forward and reverse conversion.
like:
mb_convert_encoding ("Hello", "HTML-ENTITIES", "gb2312"); //Output: 你好
mb_convert_encoding ("你好", "gb2312", "HTML-ENTITIES"); //Output: Hello
If you need to convert the entire page, you only need to add these three lines of code to the head of the php file:
mb_internal_encoding("gb2312"); // gb2312 here is the original encoding of your website
mb_http_output("HTML-ENTITIES");
ob_start('mb_output_handler');
Asp versionYou can use the following function to achieve this conversion:
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 version
function htmlentities(str)
{
var r = "";
for( i=0; i
temp = str.charCodeAt(i);
r += ""+temp+";";
}
// You can also use a regular expression to solve it
// r = str.replace(/[\d\D]/g, function($0) { return "" + $0.charCodeAt(0) + ";"; });
Return r;
}
asp.net (c#) version
private string GetHtmlEntities(string str)
{
String r = string.Empty;
for (int i = 0; i < str.Length; i++)
{
r += ""+Char.ConvertToUtf32(str,i)+";";
}
return r;
}
Related documents: Commonly used HTML character entities in web pages
The above is the detailed content of HTML entities and web page coding. For more information, please follow other related articles on the PHP Chinese website!