PHP htmlspecialchars
The function of PHP htmlspecialchars function is to be able to convert specific symbols into HTML entity symbols to solve the occasional data display problems caused by HTML special tags, such as single quotation mark ('), double quotation mark ("), greater than (>) , Less than (<) or the string connection symbol (&) is converted into HTML that can only be used for display . Why do you need such a conversion? Mostly for safety, such as entering these special symbols in the URL or search box, it is very possible Trigger the PHP script on the server side to retrieve the MySQL database, and use the PHP htmlspecialchars function to convert the data before access to a safer symbol to avoid unexpected results caused by unnecessary data access actions.
PHP htmlspecialchars Basic grammar
string htmlspecialchars( $string , $quote_flags , $encoding , $double_encode )
Parameters Table| $string | Required items are the strings to be converted . |
| $quote_flags | Optional items are used to set the conversion rules of quotation marks. The following three items are common setting parameters.
|
| $encoding | Select the item and set the encoding for conversion. The default encoding for versions before PHP 5.4.0 is ISO-8859-1, and the version after PHP 5.4.0 is default for Universal Code UTF-8. |
| $double_encode | Select the item, if this function is turned off, htmlspecialchars will not convert the existing HTML symbols, the default is to convert all. |
PHP htmlspecialchars conversion result
- The string connection symbol (&) is converted to &
- Double quotation marks (") are converted to ". The preceding question is that the quotation mark parameter cannot be set to ENT_NOQUOTES.
- Single quote (') is converted to '. Only when the quotation marks parameter is set to ENT_QUOTES, will single quotation marks be converted.
- The greater than sign (>) is converted to >
- The less than sign (<) is converted to <
$string_1 = htmlspecialchars("This is a test\'This is a test\"", ENT_COMPAT);
echo $string_1.'<br>';
$string_2 = htmlspecialchars("This is a test\'This is a test\" ", ENT_QUOTES);
echo $string_2.'<br>';
$string_3 = htmlspecialchars("This is a test\'This is a test\"", ENT_NOQUOTES);
echo $string_3.'<br>';
?>
This is a test\'This is a test"
This is a test\'This is a test"
Post a Comment
0 Comments