About the use of htmlentities, Htmlspecialchars, addslashes

Source: Internet
Author: User

1. Html_entity_decode (): Converts an HTML entity to a character.

Eg: $str = "Just atest & & #039; Learn to use & #039; ";

echo Html_entity_decode ($STR);

echo "<br/>";

Echo Html_entity_decode ($str, ent_quotes);

echo "<br/>";

Echo Html_entity_decode ($str, ent_noquotes);

The output is as follows:

Just a Test & ' learn to use '
Just a Test & ' learn to use '
Just a Test & ' learn to use '

2. Htmlentities (): Converts a character to an HTML entity.

Eg: $str = "Just a Test & ' learn to use '";

Echo htmlentities ($str, Ent_compat);

echo "<br/>";

Echo htmlentities ($str, ent_quotes);

echo "<br/>";

Echo htmlentities ($str, ent_noquotes);

The output is as follows:

Just a Test & ' learn to use '
Just a Test & ' learn to use '
Just a Test & ' learn to use '

View the source code as follows:

Just a test &amp; ' Learn to use ' <br/>

Just a test &amp; & #039; learn to use& #039; <br/>

Just a test &amp; ' Learn to use '

3. Addslashes (): Adds a backslash before the specified predefined character

Predefined characters include: Single quotation mark ('), double quotation mark ("), backslash (\), NULL

By default, PHP instruction MAGIC_QUOTES_GPC is on, and automatically runs Addslashes () for all get, POST, and cookie data. Do not use Addslashes () for strings that have been MAGIC_QUOTES_GPC escaped, because this results in double-layer escaping. You can use the function GET_MAGIC_QUOTES_GPC () to detect this situation.

Eg: $str 3= "\ just a" \ "test";

Echoaddslashes ($str 3);

Output:

\ \ just a \ ' \ "Test

4. Stripslashes (): Remove the backslash added by the addslashes function

Eg: $str 4= "\ \ Just a \ '" Test ";

Echo stripslashes ($str 4);

Output:

Just a ' "test

5, Htmlspecialchars (): Convert some of the predefined characters to HTML entities.

Predefined characters including:& (and) become &amp;
"(double quotes) become &quot;
' (single quotes) become & #039;
< (less than) becomes &lt;
> (greater than) becomes &gt;

Eg: $str 5 = "Just atest & ' Learn to use '";

Echo Htmlspecialchars ($str 5, Ent_compat);

echo "<br/>";

Echo Htmlspecialchars ($str 5, ent_quotes);

echo "<br/>";

Echo Htmlspecialchars ($str 5, ent_noquotes);

Output:

Just a Test & ' learn to use '
Just a Test & ' learn to use '
Just a Test & ' learn to use '

View Source code: Just a Test &amp; ' Learn to use ' <br/>
Just a test &amp; & #039; learn to use& #039; <br/>
Just a test &amp; ' Learn to use '
6, Htmlspecialchars_decode (): Convert some of the predefined HTML entities to characters.

HTML entities that will be decoded include:&amp; Become & (and No.)
&quot; Become "(double quotes)
& #039; Become ' (single quote)
&lt; Become < (less than)
&gt; Become > (greater than)

Eg: $str 6 = "Just atest &amp; & #039; learn to use& #039; ";

Echo Htmlspecialchars_decode ($str 6);

echo "<br/>";

Echo Htmlspecialchars_decode ($str 6, ent_quotes);

echo "<br/>";

Echo Htmlspecialchars_decode ($str 6, ent_noquotes);

Output:
Just a Test & ' learn to use '
Just a Test & ' learn to use '
Just a Test & ' learn to use '
View Source code:
Just a Test & & #039; Learn to use & #039; <br/>
Just a Test & ' learn to use ' <br/>
Just a Test & & #039; Learn to use & #039;


So far, I think we should understand the basic trial of several functions.

About the use of htmlentities, Htmlspecialchars, addslashes

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.