I. htmlentities () and Htmlspecialchars ()
1, Htmlentities ()
1.1 Features: Converts characters to HTML entities. Characters include ASCII entities and ISO 8859-1 entities (HTML entity tables: 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 remaining optional, quotestyle rules how to encode single and double quotes: ent_compat– default. Encode only double quotes; ent_quotes– encoded double quotes and single quotes; ent_noquotes– does not encode any quotes. Character-set is a set of character sets for conversion purposes, commonly used with utf-8/gb-2312/iso-8859-1 (default).
1.4 Hint: The character set that cannot be recognized will be ignored and replaced by Iso-8859-1.
$str = "John & ' Adams";
echo htmlentities ($STR);
Output in Browser: John & ' Adams '
View Source code: John & ' Adams '
2, Htmlspecialchars ()
2.1 Converts some predefined characters to HTML entities. Predefined characters are ASCII entities, that is, this 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) become <
> (greater than) become >
2.2 Htmlspecialchars (String,quotestyle,character-set)
2.3 Parameter Htmlentities ()
2.4 Hint: The character set that cannot be recognized will be ignored and replaced by Iso-8859-1.
$str = "John & ' Adams";
echo htmlentities ($STR);
Output in Browser: John & ' Adams '
View Source code: John & ' Adams '
II. Html_entity_decode () and Htmlspecialchars_decode ()
The Html_entity_decode (string,quotestyle,character-set) function converts an HTML entity to a character and is an inverse of htmlentities ().
The Htmlspecialchars_decode (string,quotestyle) function converts a predefined HTML entity to a character, which is the inverse of htmlspecialchars ().
$str = "John & & #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. 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 quotes ('), double quotes ("), reverse skew (), and null
PS: By default, the PHP directive MAGIC_QUOTES_GPC to on and automatically runs Addslashes () for all get, POST, and COOKIE data. Do not use Addslashes () on strings that have been escaped by MAGIC_QUOTES_GPC, because this can result in a double escape. You can use the function GET_MAGIC_QUOTES_GPC () for instrumentation when this situation is encountered.
$str = "Who ' s John Adams?";
Echo $str. "This isn't safe in a database query.<br/>";
echo addslashes ($STR). "This is safe in a database query.";
Output:
Who ' s John Adams? This isn't safe in a database query.
Who ' s John Adams? This is safe in a database query.
2. Addcslashes (string,characters) function adds a backslash before the specified character. Stirng must, the second optional. Specify the character or range of characters affected by addcslashes ().
PS: Be careful when applying addcslashes () to 0,r,n and T. In PHP, R,n and T are predefined escape sequences. This function can be added to any character, including predefined characters, which is different from the Addslashes
To add a backslash to a specific character
$str = "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 name is John Adams.
Hello, my name is John Adams.
Adds a backslash to a range of characters in a string
$str = "Hello, my name is John Adams";
Echo $str;
echo addslashes ($STR); Using 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.
Hello, my name is John Adams.
Hello, my name is John Adams.