Use of Htmlentities, Addslashes, Htmlspecialchars

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 characters to HTML entities.

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 include:& (and number) 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 '

                &NBS P    just a test   &amp; & #039; learn to use& #039; <br/>
                     just a test  &amp; ' Learn to use '
6, htmlspecialchars_decode (): Converts some of the predefined HTML entities to characters.

 &quot; becomes "(double quotes)
 & #039; become ' (single quote)
 &lt; Become < (less than)
 &gt; becomes > (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 you should understand the basic trial of several functions.

Use of Htmlentities, Addslashes, Htmlspecialchars

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.