The htmlspecialchars()
function in PHP is used to convert special characters to HTML entities. This is commonly used to prevent Cross-Site Scripting (XSS) attacks by making sure that HTML tags or JavaScript code entered by users are displayed as text and not executed by the browser.
To make sure that:
<
, >
, "
and '
are not interpreted as HTML or JavaScript.$user_input = "<script>alert('XSS');</script>";
$safe_output = htmlspecialchars($user_input);
echo $safe_output;
// Output: <script>alert('XSS');</script>
Flag | Description |
---|---|
ENT_COMPAT |
Converts double quotes only (default). |
ENT_QUOTES |
Converts both double and single quotes. |
ENT_NOQUOTES |
Does not convert any quotes. |
ENT_HTML401 |
Handle HTML 4.01 (default). |
ENT_HTML5 |
Use for HTML5 documents. |
htmlspecialchars()
when displaying user input in HTML.htmlspecialchars_decode()
if you need to revert the conversion.