PHP get_html_translation_table() Function

What is PHP get_html_translation_table() Function?

Translation table refers to an array that maps one set of values to another. This table helps to define exactly which characters in a given string need to be converted into a safe HTML entities.

So, if you want to get the translation table used by the htmlspecialchars() and htmlentities() functions, use PHP get_html_translation_table() function.

Syntax:

get_html_translation_table(function,  flags, character-set)

Parameters:

The Function has 3 optional parameters-

function (Optional): It specifies which translation table to use. It has possible 2 values-

  • HTML_SPECIALCHARS (default value) or
  • HTML_ENTITIES

flags (Optional): It specifies how to handle quotes, invalid encoding and the used document type. The default is ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401.

encoding (Optional): It specifies the encoding used when converting characters. If omitted, encoding defaults to the value is UTF-8.

Return Values:

The function returns the translation table as an array, with the original characters as keys and entities as values.

Examples:

Example 1:

<pre>
<?php
$table = get_html_translation_table();
print_r($table);
?>
</pre>

Output:

Array
(
["] => "
[&] => &
['] => '
[<] => &lt;
[>] => &gt;
)

Explanation:

In the view source of the browser, you’ll see this.

Example 2:

&lt;pre>
&lt;?php
$table = get_html_translation_table(HTML_ENTITIES, ENT_QUOTES | ENT_HTML401);
print_r($table);
?>
&lt;/pre>

Output:

Array
(
["] => &quot;
[&] => &amp;
['] => &#039;
[<] => &lt;
[>] => &gt;
[ ] => &nbsp;
[¡] => &iexcl;
[¢] => &cent;
[£] => &pound;
[¤] => &curren;
[¥] => &yen;
[¦] => &brvbar;
[§] => &sect;
[¨] => &uml;
…………

Explanation:

In the view source of the browser, you’ll see this.

PHP Version Support:

PHP 4, PHP 5, PHP 7, PHP 8

Summary: PHP get_html_translation_table() Function

The get_html_translation_table() function is one of the built-in string functions. Use this function to get the translation table used by the htmlspecialchars() and htmlentities() functions.

Reference:

https://www.php.net/manual/en/function.get-html-translation-table.php