Three groups of functions that I often easily confuse in PHP

Source: Internet
Author: User

Original: http://www.ido321.com/1252.html

I, Htmlentities () and Htmlspecialchars ()

1, Htmlentities ()

1.1 function: Convert characters to HTML entities. characters include ASCII entities and ISO 8859-1 entities (HTML entity table: http://www.w3school.com.cn/tags/html_ref_entities.html)

1.2 Syntax: htmlentities (String,quotestyle,character-set)

1.3 Parameter: String is a required parameter and is a string that needs to be converted. The rest is optional, quotestyle rules how to encode single and double quotes: ent_compat– default. Encodes double quotation marks only; ent_quotes– encodes double and single quotes; ent_noquotes– does not encode any quotation marks. Character-set is a set of character sets used for conversion, often with utf-8/gb-2312/iso-8859-1 (default).

1.4 Hint: The unrecognized character set will be ignored and replaced by Iso-8859-1.

"John & ' Adams '"; echo htmlentities ($STR); //output in the browser: John & ' Adams ' ' Adams '

2, Htmlspecialchars ()

2.1 Convert some of the predefined characters to HTML entities. The predefined characters are ASCII entities , which means that the function cannot convert ISO 8859-1 entities, which is the difference from htmlrntities ()

The predefined characters are:

    • & (and number) becomes &
    • "(double quotes) become "
    • ' (single quotes) become & #039;
    • < (less than) becomes &lt;
    • > (greater than) becomes &gt;

2.2 Htmlspecialchars (String,quotestyle,character-set)

2.3 Parameter Htmlentities ()

2.4 Hint: The unrecognized character set will be ignored and replaced by Iso-8859-1.

"John & ' Adams '"; echo htmlentities ($STR); //output in the browser: John & ' Adams ' ' Adams '

II, Html_entity_decode () and Htmlspecialchars_decode ()

The Html_entity_decode (string,quotestyle,character-set) function converts an HTML entity to a character, which is an inverse function of the htmlentities ().

The Htmlspecialchars_decode (string,quotestyle) function converts a predefined HTML entity to a character, which is an inverse function of htmlspecialchars ().

"John &amp; & #039; adams& #039; "; echo Html_entity_decode ($STR); //Browser output: John & ' Adams '//Source code: John & & #039; adams& #039;

III, Addslashes () and Addcslashes ()

1, Addslashes (string): Adds a backslash before the specified predefined character . The string is the one that needs to be checked. The number of functions can be used to prepare the appropriate string for strings stored in the database and for database query statements.

Predefined characters are: single quotation mark ('), double quotation mark ("), backslash (\), and null

PS: 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.

"Who ' s John Adams?"; Echo "This isn't safe in a database query.<br/>"; Echo "This is safe in a database query.";

Output:

Who ' s John Adams? This is the not safe in a database query. Who\ ' s John Adams? This is safe in a database query.
   2, the Addcslashes (string,characters) function adds a backslash before the specified character . Stirng must be the second optional. Specifies the range of characters or characters affected by addcslashes (). 
             PS: be careful when applying addcslashes () to 0,r,n and T. In PHP, \0,\r,\n and \ t are pre-defined escape sequences. This function can be added to any character, including pre-defined characters, which is the difference from addslashes
//Add a backslash to a specific character  "Hello, my name is John Adams."; echo $str; echo addcslashes ($str,' m '); echo addcslashes ($str,' J ');

Output:

Hello, my name is John Adams.hello, \my Na\me is John Ada\ms. Hello, my name is \john Adams.
//Add a backslash to a range of characters in a string$str = "Hello, my name is John Adams."; echo $str;   echo addslashes ($STR); //Use addslashes echo addcslashes ($str,' A. Z '); echo addcslashes ($str,' A. Z '); echo addcslashes ($str,' A. H ');

Output:

Hello, my name is John Adams.

Hello, my name is John Adams.

\hello, my name is \john \adams.

H\e\l\l\o, \m\y \n\a\m\e \i\s j\o\h\n a\d\a\m\s.

H\ello, my n\am\e is Jo\hn a\d\ams.

Next:install xampp under Ubuntu

Three groups of functions that I often easily confuse in PHP

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.