php8.5
Home/ Manual/ strings / functions/ get_html_translation_table

get_html_translation_table

PHP function Edit on GitHub ✎

(PHP 4, PHP 5, PHP 7, PHP 8)

Returns the translation table used by htmlspecialchars and htmlentities

Description

get_html_translation_table(int $table = HTML_SPECIALCHARS, int $flags = ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401, string $encoding = "UTF-8"): array

get_html_translation_table() will return the translation table that is used internally for htmlspecialchars() and htmlentities().

Note

Special characters can be encoded in several ways. E.g. " can be encoded as ", " or ". get_html_translation_table() returns only the form used by htmlspecialchars() and htmlentities().

Parameters

table

Which table to return. Either HTML_ENTITIES or HTML_SPECIALCHARS.

flags

A bitmask of one or more of the following flags, which specify which quotes the table will contain as well as which document type the table is for. The default is ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401.

Constant NameDescription
ENT_COMPATTable will contain entities for double-quotes, but not for single-quotes.
ENT_QUOTESTable will contain entities for both double and single quotes.
ENT_NOQUOTESTable will neither contain entities for single quotes nor for double quotes.
ENT_SUBSTITUTEReplace invalid code unit sequences with a Unicode Replacement Character U+FFFD (UTF-8) or � (otherwise) instead of returning an empty string.
ENT_HTML401Table for HTML 4.01.
ENT_XML1Table for XML 1.
ENT_XHTMLTable for XHTML.
ENT_HTML5Table for HTML 5.
encoding

Encoding to use. If omitted, the default value for this argument is UTF-8.

Charsets

Return Values

Returns the translation table as an array, with the original characters as keys and entities as values.

Changelog

VersionDescription
8.1.0flags changed from ENT_COMPAT to ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401.

Examples

Translation Table Example

php
<?php
var_dump(get_html_translation_table(HTML_ENTITIES, ENT_QUOTES | ENT_HTML5));
?>

The above example will output something similar to:

output
array(1510) {
  ["
"]=>
  string(9) "&NewLine;"
  ["!"]=>
  string(6) "&excl;"
  ["""]=>
  string(6) "&quot;"
  ["#"]=>
  string(5) "&num;"
  ["$"]=>
  string(8) "&dollar;"
  ["%"]=>
  string(8) "&percnt;"
  ["&"]=>
  string(5) "&amp;"
  ["'"]=>
  string(6) "&apos;"
  // ...
}

See Also

Source: reference/strings/functions/get-html-translation-table.xml · from the official PHP manual (php/doc-en)