PHP String htmlspecialchars() Function
The htmlspecialchars() function converts special characters into HTML entities. It is the in-built function of PHP, which converts all pre-defined characters to the HTML entities. The pre-defined characters are:
- & (ampersand) converted as &
- “ (double quote) converted as "
- ‘ (single quote) converted as '
- < (less than) converted as <
- > (greater than) converted as >
There is a string function htmlspecialchars_decode(), which is reverse of the htmlspecialchars() function. The main purpose of htmlspecialchars_decode() function is to convert special HTML entities back to characters. htmlspecialchars() and htmlspecialchars_decode() function are opposite to each other. The syntax of the htmlspecialchars() function is given below:
Syntax:
Parameters
$string: This parameter is contains the input string.
$flags: Basically, this parameter is used to hold the one or more flags from following, which specify how to handle invalid code unit sequences, quotes, and the used document type. ENT_COMPAT | ENT_HTML401 is by default. The available flags constants are given below in the table:
Available flags constants
Constant Name | Description |
---|