PHP Reference ยท String Functions
PHP htmlspecialchars() function
Reference for PHP htmlspecialchars(): escape &, <, >, quotes for safe HTML output, the flags, double_encode, invalid UTF-8 and why it prevents XSS.
htmlspecialchars() converts the characters that have a special meaning in HTML into entities: & becomes &, < becomes <, > becomes >, and quotes become " and '. Passing text through it before you print it into a page is the standard defense against cross-site scripting (XSS), because the browser then shows <script> as text instead of running it.
Escape at the moment of output, and escape for the place you print in. This function is correct for HTML text and for quoted attribute values. It is not enough for JavaScript, CSS or URLs.
<?php echo htmlspecialchars('<a href="x">Tom & Jerry</a>'); // <a href="x">Tom & Jerry</a>
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Syntax
htmlspecialchars( string $string, int $flags = ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401, ?string $encoding = null, bool $double_encode = true ): string
| Name | Description |
|---|---|
$string |
The text to escape. |
$flags |
A bit mask that chooses which quotes are converted and how invalid input is handled. |
$encoding |
Character set of the input. null uses default_charset, which is UTF-8. |
$double_encode |
If false, existing entities such as & are left alone instead of becoming &amp;. |
Common flags:
| Flag | Effect |
|---|---|
ENT_COMPAT |
Converts double quotes only. |
ENT_QUOTES |
Converts both double and single quotes. |
ENT_NOQUOTES |
Converts neither. |
ENT_SUBSTITUTE |
Replaces invalid UTF-8 with U+FFFD instead of returning an empty string. |
ENT_HTML5 / ENT_HTML401 / ENT_XML1 |
Selects the entity set; in HTML5 mode a single quote becomes ', in HTML 4.01 mode '. |
Return value: the escaped string.
Usage notes
- Defaults changed in PHP 8.1. Before 8.1 the default flags were
ENT_COMPAT | ENT_HTML401, so single quotes were not escaped, which could break attributes written with single quotes. Pass the flags explicitly if your code must behave the same everywhere. - Only five characters. It touches
& " ' < >. Other characters such as accents stay as they are; usehtmlentities()only if you really need named entities, which is rare with UTF-8. - Escape on output, not on input. Store the original text in the database and escape when you render, so each place (HTML, JSON, CSV) can apply its own encoding.
- Not for every context. Inside a
<script>block usejson_encode()with theJSON_HEX_*flags, for URL parts userawurlencode(), and never place user input into an unquoted attribute, anhrefwith ajavascript:URL or an inline event handler. - Wrong encoding gives an empty string. Without
ENT_SUBSTITUTE, invalid UTF-8 makes the function return"". The default flags include it since 8.1. - Null. Passing
nullis deprecated since PHP 8.1; cast or use?? "". double_encode. Leave ittruefor untrusted text. Setting it tofalseis only for text that is already partly escaped, and it is a common source of mistakes.- Reverse it with
htmlspecialchars_decode().