PHP htmlspecialchars() Function

Last Updated:

PHP htmlspecialchars()

The htmlspecialchars() function converts special characters to HTML entities.

The pre-defined characters are:

  • & (ampersand) becomes &
  • " (double quote) becomes "
  • ' (single quote) becomes '
  • < (less than) becomes &lt;
  • > (greater than) becomes &gt;

Tips: If you require all input substrings that have associated named entities to be translated, use htmlentities() instead.


PHP Compiler
<?php $str = "This is <u>underline</u>."; echo htmlspecialchars($str) . "<br>"; echo ($str); ?>


htmlspecialchars(str, flag, character-set, double_encode)

Parameter Values

str Required Specifies the input string.
flag Optional Specifies how to handle quotes, invalid code unit sequences and the used document type.
Possible Values:
  • ENT_COMPAT - Table will contain entities for double-quotes, but not for single-quotes.
  • ENT_QUOTES - Table will contain entities for both double and single quotes.
  • ENT_NOQUOTES - Table will neither contain entities for single quotes nor for double quotes.
  • ENT_SUBSTITUTE - Replace invalid code unit sequences with a Unicode Replacement Character U+FFFD (UTF-8) or &#FFFD; (otherwise) instead of returning an empty string.
  • ENT_DISALLOWED - Replace invalid code points for the given document type with a Unicode Replacement Character U+FFFD (UTF-8) or &#FFFD; (otherwise) instead of leaving them as is.
  • ENT_HTML5 - Handle code as HTML 5.
  • ENT_HTML401 - Handle code as HTML 4.01.
  • ENT_XML1 - Handle code as XML 1.
  • ENT_XHTML - Handle code as XHTML.
Default value is ENT_COMPAT.
character-set Optional Specifies the character-set to be used when converting characters.
Possible Values:
  • ISO-8859-1 - Western European, Latin-1.
  • ISO-8859-5 - Little used cyrillic charset (Latin/Cyrillic).
  • ISO-8859-15 - Western European, Latin-9. Adds the Euro sign, French and Finnish letters missing in Latin-1 (ISO-8859-1).
  • UTF-8 - ASCII compatible multi-byte 8-bit Unicode.
  • cp866 - DOS-specific Cyrillic charset.
  • cp1251 - Windows-specific Cyrillic charset.
  • cp1252 - Windows specific charset for Western European.
  • KOI8-R - Russian.
  • BIG5 - Traditional Chinese, mainly used in Taiwan.
  • GB2312 - Simplified Chinese, national standard character set.
  • BIG5-HKSCS - Big5 with Hong Kong extensions, Traditional Chinese.
  • Shift_JIS - Japanese
  • EUC-JP - Japanese
  • MacRoman - Charset that was used by Mac OS.
Default value is 'UTF-8' (PHP 5.4.0 and above).
double_encode Optional Specifies whether to encode existing html entities or not.
  • TRUE - Will convert everything
  • FALSE - Will not encode existing html entities
Default value is TRUE.

Return Value

String Returns the converted string.

Share this Page

Meet the Author