Conversion of special HTML elements in PHP

Source: Internet
Author: User
Tags string back

Some special characters are often processed during PHP project development. Otherwise, these characters may not be output or garbled. Some common special characters in HTML tags are converted.
Q & to & amp;
Q "to & quot;
Q 'to & #039;
Q <to & lt;
Q> convert to & gt;


"&" Indicates the HTML element and "& amp;" indicates the displayed string. For example, if you want to output the following code normally on the page:
<A href = 'HTTP: // www.mingrisoft.com '> tomorrow technology </a>
You need to enter in the HTML code: "& lt; a href = & #039; http://www.mingrisoft.com & #039; & gt; tomorrow Technology & lt;/A & gt ;", otherwise, the output cannot be based on the original meaning.
PHP provides the following functions to automatically convert to HTML elements.
1. htmlspecialchars () function
This function can convert certain characters to display in HTML. Syntax:
String htmlspecialchars (string STR [, int quote_style [, string charset]);
This function converts special characters into HTML strings (&....;). This function only converts the five special characters listed above.
Example:
Use the htmlspecialchars () function to convert special characters in the message to HTML string format. The program code is as follows:
<? PHP
$ Word = "& lt; html> Echo htmlspecialchars ("$ word ");
?>
Result of this example: <HTML> Note: Because the htmlspecialchars () function is added to the source code, the code is not interpreted and executed in the message.
2. htmlentities () function
This function is used to convert all HTML elements into display strings. Syntax:
String htmlentities (string STR [, int quote_style [, string charset]);
3.html _ entity_decode () function
This function is used to convert a display string to an HTML element. Syntax:
String html_entity_decode (string STR [, int quote_style [, string charset]);
STR: the original string.
Quote_style: an optional parameter. The value range is as follows:
Q ent_compat: only four types of symbols are converted: &, ", <,>.
Q ent_quotes: only five types of symbols are converted: &, ', ", <,>.
Q ent_noq uotes: only three types of symbols are converted: &, <,>.
Charset: Specifies the character set applied during the conversion process.
Table 1 shows the character set parameters supported by PHP 4.0 and later.
Table 1 character set parameters supported by PHP 4.0 and later
Character Set
Other names
Description
ISO-8859-1
ISO-8859-1
Western European character set
ISO-8859-15
ISO-8859-15
Western European character set Extension
UTF-8
ASCII character set
KOI8-R
Koi8-ru, koi8r.
Russia
Cp1252
Windows-1252, 1252
Western European character set, default in Windows
Gb2312
936
Simplified Chinese, International Standard Character Set
Shift-JIS
Sjis, 932.
Japanese
Eucjp
Eucjp
Japanese
Example:
Use the htmlentities () function to obtain the display string of an HTML statement, and then use the html_entity_decode () function to re-convert the display string back to the HTML element. The program code is as follows:
<? PHP
$ Word = "<font color = '#886699'> there are always many things you want to hear </font> ";
Echo htmlentities ($ word, ent_compat, "gb2312"). "<br> ";
Echo html_entity_decode ($ word );
?>
The running result of this example is as follows:
<Font color = '#886699'> there are always many things you want to hear </font>
There are always many things you want to hear

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.