Html Escape characters

Source: Internet
Author: User

This article is published in sync with the tiankonguse blog


Today I encountered the problem of html character escaping, So I recorded it.


Problem Reproduction


You can add a title field to the link to describe the link.

However, the characters in this title are strictly restricted and cannot contain characters such as quotation marks or spaces. Therefore, I need to escape the characters.

This title is automatically generated using php in the background, so you need to use php to escape the string.


Solution


Php has a string replacement function str_replace which is very convenient.

Let's take a look at my character escape code.


$_title = str_replace( array("\""," ","<",">","&"), array("""," ","<",">","&"),$title);


This line of code can escape some characters.

The first parameter indicates the search character, the second parameter indicates the replacement character, and the third parameter indicates the original string.


HTML character extension knowledge


The Escape string (Escape Sequence) is also called the Character Entity ).

In HTML, there are two reasons for defining escape strings: The first reason is that symbols such as "<" and ">" are already used to represent HTML tags, therefore, it cannot be used directly as a symbol in the text. To use these symbols in HTML documents, you need to define its escape string. When the interpreter encounters such a string, it is interpreted as a real character. When you enter an escape string, you must strictly follow the uppercase/lowercase rules. The second reason is that some characters are not defined in the ASCII character set, so escape strings must be used for representation.


Composition of escape strings


The Character Entity is divided into three parts: The first part is an & symbol, which is called ampersand; the second part is an Entity) name or # With the Entity number; the third part is a semicolon.

For example, to display a smaller sign (<), you can write & lt; or & #60 ;.


List of special HTML Escape characters


Display Description Entity name Entity ID
Blank & Ensp; & #8194;
Wide white space & Amp; emsp; & #8195;
Blank rows & Nbsp; & #160;
< Less & Lt; & #60;
> Greater & Gt; & #62;
& & Symbol & Amp; & #38;
" Double quotation marks & Quot; & Amp; #34;
? Copyright & Copy; & #169;
? Registered Trademark & Reg; & #174;
? Trademark (USA) ? & #8482;
× Multiplication number & Times; & #215;
Bytes Division Number & Divide; & #247;



JavaScript Escape Character

Escape Sequence Character
\ B Return
\ F Take the form of paper
\ N Line feed
\ R Enter
\ T Horizontal hop (Ctrl-I)
\' Single quotes
\" Double quotation marks
\\ Backslash


References

Http://114.xixik.com/character/

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.