HTML entities and web page coding

巴扎黑
Release: 2017-04-05 11:46:02
Original
2127 people have browsed it

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!