Getting to combat web front end: HTML character entities, escape the string

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/wewfdf/article/details/102631996

Why use escape strings?

** HTML, <,>, & other special meaning (<,>, check for a link, for & escape), ** not be used directly. These symbols are not displayed on the page to see where we end up, that if we want to display these symbols on the page, how to do it?

This comes to HTML escaped string (Escape Sequence) a.

Escape string (Escape Sequence), also known as character entities (Character Entity). In the HTML, there are two reasons defined escape character string:

The first reason is like "<" and ">" These signs are used to denote HTML tags and therefore can not be directly used as symbols in the text to use. In order to use these symbols in an HTML document, you need to define it escaped string. When the interpreter encounters such strings put it interpreted as a real character. When entering escaped string, to strictly abide by the rules of capitalization .

The second reason is that some characters in the ASCII character set is not defined, it is necessary to use the escape string to represent.

Composition escape string

Escape strings (Escape Sequence), i.e. entity character (Character Entity) is divided into three parts:

The first part is an ampersand, English called ampersand;

The second part is the entity (Entity) or the name of # plus entity (Entity) number;

The third part is a semicolon.

For example, to display less than (<), can be written <or <.

Benefits Entity (Entity) name is easier to understand, to see lt, probably guess is less than the meaning, but the disadvantage is that not all browsers support the latest Entity name. The entity (Entity) number, various browsers can handle.

Tip: name of the entity (Entity) is case-sensitive .

Note: The same symbol can be referenced with the "real name" and "real number" two ways, "the name of the entity" has the advantage of easy to remember, but can not guarantee that all browsers can successfully recognize it, while "real number" there is no such fears, but it is really hard to remember.

How to display blank?

In general, HTML is automatically cut off the excess space. No matter how much space you add, it is seen as a space. For example, you add 10 spaces between words, HTML truncates nine spaces, but one. In order to increase the space on the page, you can use indicates a space.


HTML special escape characters

The most common character entity

Character Entities

ISO 8859-1 (Latin-1) character set

HTML 4.01 supports ISO 8859-1 (Latin-1) character set.

Note: For convenience, the following table, the "entity name" referred to as the "Name", "real number" simply "No."

Mathematics and Greek letters logo

symbols, mathematical symbols, and Greek letters

Important international mark

markup-significant and internationalization characters


JavaScript escape

Programming time to pay attention to the special character of the problems, many problems arise because of the run-time special characters caused.

Note that, since the backslash itself as an escape character, and therefore can not directly type a backslash in the script. If you want to generate a backslash, you must type with two backslash (\).


Transcoding (to Unicode)

(Program code from the network)

Js version

<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)

vbs version

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;
}
 

如果大家对编程,web前端感兴趣,想要了解学习,打算深入了解这个行业的朋友,可以加下我们的前端学习扣qun : 784783012 ,不论你是学生还是想转行的朋友,我都欢迎,不定期分享干货,整理的一份2019最新的web前端学习资料和0基础入门教程分享给大家:学习前端我们是认真的


附:

在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’);

Guess you like

Origin blog.csdn.net/wewfdf/article/details/102631996