Skip to content
Tutorials Center
My learning

Topics

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 &amp;, < becomes &lt;, > becomes &gt;, and quotes become &quot; and &#039;. 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
<?php
echo htmlspecialchars('<a href="x">Tom & Jerry</a>');
// &lt;a href=&quot;x&quot;&gt;Tom &amp; Jerry&lt;/a&gt;

Try it

PHP What gets escaped

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.

PHP double_encode and a safe helper

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

PHP
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 &amp; are left alone instead of becoming &amp;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 &apos;, in HTML 4.01 mode &#039;.

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; use htmlentities() 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 use json_encode() with the JSON_HEX_* flags, for URL parts use rawurlencode(), and never place user input into an unquoted attribute, an href with a javascript: 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 null is deprecated since PHP 8.1; cast or use ?? "".
  • double_encode. Leave it true for untrusted text. Setting it to false is only for text that is already partly escaped, and it is a common source of mistakes.
  • Reverse it with htmlspecialchars_decode().