get_html_translation_table

(PHP 4, PHP 5)

get_html_translation_table --  Returns the translation table used by htmlspecialchars() and htmlentities()

Description

array get_html_translation_table ( [int table [, int quote_style]] )

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

There are two new constants (HTML_ENTITIES, HTML_SPECIALCHARS) that allow you to specify the table you want. Default value for table is HTML_SPECIALCHARS. And as in the htmlspecialchars() and htmlentities() functions you can optionally specify the quote_style you are working with. The default is ENT_COMPAT mode. See the description of these modes in htmlspecialchars().

注: Special characters can be encoded in several ways. E.g. " can be encoded as ", " or &#x22. get_html_translation_table() returns only the most common form for them.

例子 1. Translation Table Example

<?php
$trans
= get_html_translation_table(HTML_ENTITIES);
$str = "Hallo & <Frau> & Krmer";
$encoded = strtr($str, $trans);
?>
The $encoded variable will now contain: "Hallo &amp; &lt;Frau&gt; &amp; Kr&auml;mer".

See also htmlspecialchars(), htmlentities(), and html_entity_decode().


add a note add a note User Contributed Notes
trukin at gmail dot com
30-Oct-2006 03:25
There have been issues when hispanic websites or other websites dont use the corrent collision in mysql.

Some problems result that the accents ( ... ) result in weird characters when a backup is done and restored later on. Or when database is changed to another one.

To fix this try something like this
function accents($text){
   foreach(get_html_translation_table(HTML_ENTITIES) as $a=>$b){
       $text = str_replace($a,$b,$text);   
   }
   return $text;
}

and use as accents("Hello ....... WITH ACCENTS") and it will return the escaped string.
edwardzyang at thewritingpot dot com
23-Jul-2006 10:04
Quite disappointingly, get_html_translation_table() only gives the characters for ISO-8859-1, making it quite useless for UTF-8 or anything else like that (as a previous commenter noticed).
Patrick nospam at nospam mesopia dot com
30-May-2005 10:00
Not sure what's going on here but I've run into a problem that others might face as well...

<?php

$translations
= array_flip(get_html_translation_table(HTML_ENTITIES,ENT_QUOTES));

?>

returns the single quote ' as being equal to &#39; while

<?php

$translatedString
= htmlentities($string,ENT_QUOTES);

?>
returns it as being equal to &#039;

I've had to do a specific string replacement for the time being... Not sure if it's an issue with the function or the array manipulation.

-Pat
Alex Minkoff
19-May-2005 07:30
If you want to display special HTML entities in a web browser, you can use the following code:

<?
$entities
= get_html_translation_table(HTML_ENTITIES);
foreach (
$entities as $entity) {
  
$new_entities[$entity] = htmlspecialchars($entity);
}
echo
"<pre>";
print_r($new_entities);
echo
"</pre>";
?>

If you don't, the key name of each element will appear to be the same as the element content itself, making it look mighty stupid. ;)
ryan at ryancannon dot com
27-Jan-2005 06:05
In XML, you can't assume that the doctype will include the same character entity definitions as HTML. XML authors may require character references instead. The following two functions use get_html_translation_table() to encode data in numeric references. The second, optional argument can be used to substitute a different translation table.

function xmlcharacters($string, $trans='') {
   $trans=(is_array($trans))? $trans:get_html_translation_table(HTML_ENTITIES, ENT_QUOTES);
   foreach ($trans as $k=>$v)
       $trans[$k]= "&#".ord($k).";";
   return strtr($string, $trans);
}
function xml_character_decode($string, $trans='') {
   $trans=(is_array($trans))? $trans:get_html_translation_table(HTML_ENTITIES, ENT_QUOTES);
   foreach ($trans as $k=>$v)
       $trans[$k]= "&#".ord($k).";";
   $trans=array_flip($trans);
   return strtr($string, $trans);
}
kevin_bro at hostedstuff dot com
03-Jan-2003 10:06
Alans version didn't seem to work right. If you're having the same problem consider using this slightly modified version instead:

function unhtmlentities ($string)  {
   $trans_tbl = get_html_translation_table (HTML_ENTITIES);
   $trans_tbl = array_flip ($trans_tbl);
   $ret = strtr ($string, $trans_tbl);
   return preg_replace('/&#(\d+);/me',
     "chr('\\1')",$ret);
}
alan at akbkhome dot com
04-Jun-2002 01:00
If you want to decode all those &#123; symbols as well....

function unhtmlentities ($string)  {
   $trans_tbl = get_html_translation_table (HTML_ENTITIES);
   $trans_tbl = array_flip ($trans_tbl);
   $ret = strtr ($string, $trans_tbl);
   return  preg_replace('/\&\#([0-9]+)\;/me',
       "chr('\\1')",$ret);
}
dirk at hartmann dot net
20-Jun-2001 04:41
get_html_translation_table
It works only with the first 256 Codepositions.
For Higher Positions, for Example &#1092;
(a kyrillic Letter) it shows the same.