get_html_translation_table – PHP String Functions

Syntax :

get_html_translation_table ( strings, flags, encoding/character-set );

Description :

get_html_translation_table() function returns the translation table used by htmlspecialchars() and htmlentities().

Note : htmlentities() function is reverse of it.

Parameter :

  • strings – This is Required parameter. String which needs to be converted.
  • flags – This is an Optional parameter. The default value for flags is ENT_COMPAT | ENT_HTML401. It elaborates how to handle quotes, invalid encoding and the used document type.

[table caption=”List of available flags constants are” max-width=”100%” colwidth=”25%|75%” colalign=”left|left”]

Constant Name, Description
ENT_COMPAT, It will decodes double-quotes only.
ENT_QUOTES, It will decodes both double and single quotes.
ENT_NOQUOTES, It will not decodes both double and single quotes.
ENT_HTML401, Handle code as HTML 4.01.
ENT_XML1, Handle code as XML 1.
ENT_XHTML, Handle code as XHTML.
ENT_HTML5, Handle code as HTML 5.

  • encoding/character-set – This is an Optional parameter. A string that specifies which character-set to use.
    • If omitted, the default value of the encoding varies depending on the PHP version in use. In PHP 5.6 and later, the default_charset configuration option is used as the default value. PHP 5.4 and 5.5 will use UTF-8 as the default. Earlier versions of PHP use ISO-8859-1.Although this argument is technically optional, you are highly encouraged to specify the correct value for your code if you are using PHP 5.5 or earlier, or if your default_charset configuration option may be set incorrectly for the given input.The following character sets are supported:
    • Supported charsets are :
      [table caption=”List of Supported charsets are” max-width=”100%” colwidth=”25%|75%” colalign=”left|left”]
      Charset Aliases, Description
      ISO-8859-1,Western European Latin-1.
      ISO-8859-5 , Little used cyrillic charset (Latin/Cyrillic).
      ISO-8859-15 , Western European,
      UTF-8, ASCII compatible multi-byte 8-bit Unicode.
      cp866 ibm866, DOS-specific Cyrillic charset.
      cp1251 , Windows-specific Cyrillic charset.
      cp1252 , Windows specific charset for Western European.
      KOI8-R,  Russian.
      BIG5 950, Traditional Chinese. mainly used in Taiwan.
      GB2312 936, Simplified Chinese. national standard character set.
      BIG5-HKSCS , with Hong Kong extensions. Traditional Chinese.
      Shift_JIS , Japanese
      EUC-JP , Japanese
      MacRoman Charset, that was used by Mac OS.
      Empty string or ”, An empty string activates detection from script encoding (Zend multibyte) or default_charset and current locale (see nl_langinfo() and setlocale()) so in this order. Not recommended.

Note : Any other character sets are not recognized. The default encoding will be used instead and a warning will be emitted.

Output :

This will return a decoded string.

ChangeLog :

[table caption=”” width=”100%” colwidth=”25%|75%” colalign=”left|left”]
Version, Description
PHP 5.4.0 , The default value for the encoding parameter was changed to UTF-8.
PHP 5.4.0 ,The constants ENT_HTML401 / ENT_XML1 / ENT_XHTML / ENT_HTML5 were introduced.
PHP 5.3.4 , The encoding parameter was added.

Related articles :  htmlentities(), htmlspecialchars(), html_entity_decode().

get_html_translation_table() – PHP Functions Example 1 : Listing table for HTML_SPECIALCHARS:
print_r (get_html_translation_table()); // HTML_SPECIALCHARS is default.

See below is the output of above code in web browser.

[“] => &quot;
[&] => &amp;
[<] => &lt;
[>] => &gt;

get_html_translation_table() – PHP Functions Example 2 :
print_r (get_html_translation_table(HTML_ENTITIES));

See below is the output of above code in Web browser.

[“] => &quot;
[&] => &amp;
[<] => &lt;
[>] => &gt;
[Â ] => &nbsp;
[¡] => &iexcl;
[¢] => &cent;

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *