Categories: PHP

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.
[/table]

  • 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.
      [/table]

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.
[/table]


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


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

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

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


get_html_translation_table() – PHP Functions Example 2 :
<?php
print_r (get_html_translation_table(HTML_ENTITIES));
?>

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

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

jyoti rani

Recent Posts

What Is a Progressive Web App? Why Would You Need One?

App usage is growing steadily without showing any signs of slowing down. Hence, it is no surprise that mobile applications…

1 year ago

7 Most Popular Paid Online Advertising Strategy

As the world has grown more digital, businesses have adapted themselves. An effectual adaptation includes online advertising. Offline advertising styles…

1 year ago

The Importance of User-Centered Design in Mobile App Development

Step into a world where apps dance to the user's tune. Picture Instagram, a photo-sharing sensation that swept the globe.…

1 year ago

Healthcare Mobile App Development: A Complete Guide for Founders

COVID-19 has led to a digitalization of lifestyle. As patients are taking their mental and physical health more seriously, healthcare…

1 year ago

Exploring Diverse WordPress Theme Niches: A Comprehensive Guide

Introduction WordPress, an immensely popular content management system (CMS), powers over 40% of the internet. What makes WordPress even more…

1 year ago

8 Awesome Blog Content Ideas for Movers to Skyrocket the SEO

For moving companies trying to capture their market share amidst stiff competition, a tip or two about what they can…

1 year ago