downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

hebrev> <fprintf
[edit] Last updated: Fri, 25 May 2012

view this page in

get_html_translation_table

(PHP 4, PHP 5)

get_html_translation_tableDevuelve la tabla de traducción utilizada por htmlspecialchars() y htmlentities()

Descripción

array get_html_translation_table ([ int $table = HTML_SPECIALCHARS [, int $flags = ENT_COMPAT | ENT_HTML401 [, string $encoding = 'UTF-8' ]]] )

get_html_translation_table() devolverá la tabla de traducción que es utilizada internamente para htmlspecialchars() y htmlentities().

Nota:

Los caracteres especiales se pueden codificar de varias maneras. Por ejemplo " puede ser codificado como &quot;, &#34; o &#x22. get_html_translation_table() devuelve sólo la forma utilizada por htmlspecialchars() y htmlentities().

Parámetros

table

Cual tabla devolver. EntreHTML_ENTITIES o HTML_SPECIALCHARS).

flags

Una máscara de bits de uno o más de los siguientes indicadores, que especifican que comillas contendrá la tabla, así como para qué tipo de documento es la tabla. El valor por defecto es ENT_COMPAT | ENT_HTML401.

Constantes disponibles para flags
Nombre de la constante Descripción
ENT_COMPAT La tabla contendrá entidades para comillas dobles, pero no por comillas simples.
ENT_QUOTES La tabla contendrá entidades para ambas comillas dobles y simples.
ENT_NOQUOTES La tabla no contendrá entidades de comillas simples ni las de comillas dobles.
ENT_HTML401 Tabla para HTML 4.01.
ENT_XML1 Tabla para XML 1.
ENT_XHTML Tabla para XHTML.
ENT_HTML5 Tabla para HTML 5.

encoding

La codificación a usar. Si se omite, el valor por defecto para este argumento es ISO-8859-1 en versiones de PHP anteriores a 5.4.0, y es UTF-8 a partir de PHP 5.4.0 en adelante.

Los siguientes juegos de caracteres están soportados:

Conjunto de Caracteres Soportados
Juego de caracteres Alias Descripción
ISO-8859-1 ISO8859-1 Europeo Occidental, Latin-1.
ISO-8859-5 ISO8859-5 Juego de caracteres cirílicos poco usado (Latin/Cyrillic).
ISO-8859-15 ISO8859-15 Europeo Occidental, Latin-9. Añade el signo de Euro, y letras del Francés y Finlandés que hacián falta en Latin-1 (ISO-8859-1).
UTF-8   Multi-byte Unicode de 8-bits compatible con ASCII.
cp866 ibm866, 866 Juego de caracteres Cirílico específico de DOS.
cp1251 Windows-1251, win-1251, 1251 Juego de caracteres Cirílicos específico de Windows.
cp1252 Windows-1252, 1252 Juego de caracteres específico de Windows para Europa Occidental.
KOI8-R koi8-ru, koi8r Ruso.
BIG5 950 Chino Tradicional, usado principalmente en Taiwán.
GB2312 936 Chino Simplificado, juego de caracteres estándar nacional.
BIG5-HKSCS   Big5 con extensiones de Hong Kong, Chino Tradicional.
Shift_JIS SJIS, SJIS-win, cp932, 932 Japonés
EUC-JP EUCJP, eucJP-win Japonés
MacRoman   Juego de caracteres que fue utilizado por Mac OS.
''   Un string vacío activa la detección desde la codificación del script (Zend multibyte), default_charset y la actual configuración regional (ver nl_langinfo() y setlocale()), en este orden. No se recomienda.

Nota: Cualquier otro juego de caracteres no es reconocido. La codificación por defecto será utilizada en su lugar y se emitirá una advertencia.

Valores devueltos

Devuelve la tabla de traducción como un array, con los caracteres originales como claves y las entidades como valores.

Historial de cambios

Versión Descripción
5.4.0 El valor por defecto para el parámetro encoding fue cambiado a UTF-8.
5.4.0 Fueron agregadas las constantes ENT_HTML401, ENT_XML1, ENT_XHTML y ENT_HTML5.
5.3.4 Fue agregado el parámetro encoding.

Ejemplos

Ejemplo #1 Ejemplo de tabla de traducción

<?php
var_dump
(get_html_translation_table(HTML_ENTITIESENT_QUOTES ENT_HTML5));
?>

El resultado del ejemplo sería algo similar a:

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

Ver también



hebrev> <fprintf
[edit] Last updated: Fri, 25 May 2012
 
add a note add a note User Contributed Notes get_html_translation_table
michael dot genesis at gmail dot com 12-Dec-2011 03:01
The fact that MS-word and some other sources use CP-1252, and that it is so close to Latin1 ('ISO-8859-1') causes a lot of confusion. What confused me the most was finding that mySQL uses CP-1252 by default.

You may run into trouble if you find yourself tempted to do something like this:
<?php
    $trans
[chr(149)] = '&bull;';    // Bullet
   
$trans[chr(150)] = '&ndash;';    // En Dash
   
$trans[chr(151)] = '&mdash;';    // Em Dash
   
$trans[chr(152)] = '&tilde;';    // Small Tilde
   
$trans[chr(153)] = '&trade;';    // Trade Mark Sign
?>

Don't do it. DON'T DO IT!

You can use:
<?php
    $translationTable
= get_html_translation_table(HTML_ENTITIES, ENT_NOQUOTES, 'WINDOWS-1252');
?>

or just convert directly:
<?php
    $output
= htmlentities($input, ENT_NOQUOTES, 'WINDOWS-1252');
?>

But your web page is probably encoded UTF-8, and you probably don't really want CP-1252 text flying around, so fix the character encoding first:
<?php
    $output
= mb_convert_encoding($input, 'UTF-8', 'WINDOWS-1252');
   
$ouput = htmlentities($output);
?>
kevin at cwsmailbox dot xom 15-Sep-2010 09:55
Be careful using get_html_translation_table() in a loop, as it's very slow.
subweb007 at hotmail dot com 09-Apr-2009 01:29
This function will convert get_html_translation_table from a ISO-8859-1 string to UTF-8 string.

<?php
function translation_table_to_utf8($arTranslationtable)
{
   
//loop through the array and convert everything both keys and values
   
foreach($arTranslationtable as $charkey => $char)
    {
       
$charkey = utf8_encode($charkey);
       
$arUTFchars[$charkey]= utf8_encode($char);
    }
     return
$arUTFchars;
}

//get the translation table
$arSpecialchar     = get_html_translation_table(HTML_ENTITIES);

//call the function to convert to utf-8
$arUTFchars = translation_table_to_utf8($arSpecialchar);
print_r($arUTFchars);
?>
Kenneth Kin Lum 23-Sep-2008 05:54
to display the mapping on a webpage no matter what the server encoding is, this can be used

  echo "<pre>\n";
  echo htmlentities(print_r((get_html_translation_table(HTML_SPECIALCHARS)), true));
  echo htmlentities(print_r((get_html_translation_table(HTML_ENTITIES)), true));

since get_html_translation_table() actually gives the special chars in iso-8859-1 (Latin-1) encoding, so to see the tables correctly using

  print_r(get_html_translation_table(HTML_ENTITIES));

your server needs to give a HTTP header as iso-8859-1, unless you use header() or manually set the browser's encoding setting to iso-8859-1.  And you need to view the source of the page to see the mapping.  (except English version of IE 7 outputs the page source as iso-8859-1 anyway).
robertn972 at gmail dot com 22-Jul-2008 08:18
I found this useful in converting latin characters

<?php
function convertLatin1ToHtml($str) {
$allEntities = get_html_translation_table(HTML_ENTITIES, ENT_NOQUOTES);
$specialEntities = get_html_translation_table(HTML_SPECIALCHARS, ENT_NOQUOTES);
$noTags = array_diff($allEntities, $specialEntities);
$str = strtr($str, $noTags);
return
$str;
}
?>
adolfoabegg at gmail dot com 03-Jul-2008 12:47
"rafael at phpit dot com dot br" your solution only works for the ISO-8859-1 encoding, I mean, it works but only for that encoding and that's because get_html_translation_table won't let you specify the charset... it uses the default one, that is ISO-8859-1

The solution from "olito24 at gmx dot de" does work for UTF-8, I just modified it a bit specifying the UTF-8 charset, also the $str parameter wasn't being used at all, I just renamed it to $string

Note:
Change ENT_NOQUOTES to ENT_QUOTES to convert both double and single quotes

These are the functions to encode html but tags using UTF-8 and ISO-8859-1

<?php

class Html
{

/*by olito24 at gmx dot de*/
   
function htmlButTags($string) {       
       
       
$pattern = '<([a-zA-Z0-9\. "\'_\/-=;\(\)?&#%]+)>';
       
preg_match_all ('/' . $pattern . '/', $string, $tagMatches, PREG_SET_ORDER);
       
$textMatches = preg_split ('/' . $pattern . '/', $string);
       
        foreach (
$textMatches as $key => $value) {
           
$textMatches [$key] = htmlentities ($value, ENT_NOQUOTES, 'UTF-8');
        }
       
        for (
$i = 0; $i < count ($textMatches); $i ++) {
           
$textMatches [$i] = $textMatches [$i] . $tagMatches [$i] [0];
        }
       
        return
implode ($textMatches);
       
    }

/*by "rafael at phpit dot com dot br" */
   
function htmlButTags_iso($str){
       
// Take all the html entities
       
$caracteres = get_html_translation_table(HTML_ENTITIES,ENT_NOQUOTES);
       
// Find out the "tags" entities
       
$remover = get_html_translation_table(HTML_SPECIALCHARS,ENT_NOQUOTES);
       
// Spit out the tags entities from the original table
       
$caracteres = array_diff($caracteres, $remover);
       
// Translate the string....
       
$str = strtr($str, $caracteres);
       
// And that's it!
       
return $str;
    }
   
}

?>
Liam Morland 16-Jun-2008 08:57
Here is a simple way to convert named character entities to numeric character entities:

<?php
function numeric_entities($string){
   
$mapping = array();
    foreach (
get_html_translation_table(HTML_ENTITIES, ENT_QUOTES) as $char => $entity){
       
$mapping[$entity] = '&#' . ord($char) . ';';
    }
    return
str_replace(array_keys($mapping), $mapping, $string);
}
?>
iain (duh) workingsoftware.com.au 07-Sep-2007 02:06
I wrote a quick little function for converting something like '&middot;' into '&#183;':

$to_convert = '&middot;';
$table = get_html_translation_table(HTML_ENTITIES);
$equiv = '&#'.ord(array_search($to_convert,$table)).';';
Maurizio Siliani at trident dot it 20-Jul-2007 08:43
If you have troubles (like me) getting data from ISO-8859-1 encoded forms where user copy and paste from word, this routine could be useful.
It adds to the standard get_html_translation_table the codes of the characters usually M$ Word replacs into typed text.
Otherwise those characters would never be displayed correctly in html output.

function get_html_translation_table_CP1252() {
    $trans = get_html_translation_table(HTML_ENTITIES);
    $trans[chr(130)] = '&sbquo;';    // Single Low-9 Quotation Mark
    $trans[chr(131)] = '&fnof;';    // Latin Small Letter F With Hook
    $trans[chr(132)] = '&bdquo;';    // Double Low-9 Quotation Mark
    $trans[chr(133)] = '&hellip;';    // Horizontal Ellipsis
    $trans[chr(134)] = '&dagger;';    // Dagger
    $trans[chr(135)] = '&Dagger;';    // Double Dagger
    $trans[chr(136)] = '&circ;';    // Modifier Letter Circumflex Accent
    $trans[chr(137)] = '&permil;';    // Per Mille Sign
    $trans[chr(138)] = '&Scaron;';    // Latin Capital Letter S With Caron
    $trans[chr(139)] = '&lsaquo;';    // Single Left-Pointing Angle Quotation Mark
    $trans[chr(140)] = '&OElig;    ';    // Latin Capital Ligature OE
    $trans[chr(145)] = '&lsquo;';    // Left Single Quotation Mark
    $trans[chr(146)] = '&rsquo;';    // Right Single Quotation Mark
    $trans[chr(147)] = '&ldquo;';    // Left Double Quotation Mark
    $trans[chr(148)] = '&rdquo;';    // Right Double Quotation Mark
    $trans[chr(149)] = '&bull;';    // Bullet
    $trans[chr(150)] = '&ndash;';    // En Dash
    $trans[chr(151)] = '&mdash;';    // Em Dash
    $trans[chr(152)] = '&tilde;';    // Small Tilde
    $trans[chr(153)] = '&trade;';    // Trade Mark Sign
    $trans[chr(154)] = '&scaron;';    // Latin Small Letter S With Caron
    $trans[chr(155)] = '&rsaquo;';    // Single Right-Pointing Angle Quotation Mark
    $trans[chr(156)] = '&oelig;';    // Latin Small Ligature OE
    $trans[chr(159)] = '&Yuml;';    // Latin Capital Letter Y With Diaeresis
    ksort($trans);
    return $trans;
}
yes at king22 dot com 10-Apr-2007 08:33
Searching for a fast replacement of the MS WORD special characters which are not covered by get_html_translation_table() , I think the following function might help someone

<?php
function clean_up($str){
$str = stripslashes($str);
$str = strtr($str, get_html_translation_table(HTML_ENTITIES));
$str = str_replace( array("\x82", "\x84", "\x85", "\x91", "\x92", "\x93", "\x94", "\x95", "\x96""\x97"), array("&#8218;", "&#8222;", "&#8230;", "&#8216;", "&#8217;", "&#8220;", "&#8221;", "&#8226;", "&#8211;", "&#8212;"),$str);
return
$str;
}
?>

It replaces all types of quotes (single and double), horizontal ellipsis (...), bullet, en dash and em dash.
chris 21-Feb-2007 05:49
A lot of quite common characters (or at least not rare, like oelig, euro or minus) are missing from the table unfortunately.
Here are some, if you want to make your translation table more complete and your xml data less error-prone. Not sure why some characters have 2 codes, just use one. Here goes: '&apos;'=>'&#39;', '&minus;'=>'&#45;', '&circ;'=>'&#94;', '&tilde;'=>'&#126;', '&Scaron;'=>'&#138;', '&lsaquo;'=>'&#139;', '&OElig;'=>'&#140;', '&lsquo;'=>'&#145;', '&rsquo;'=>'&#146;', '&ldquo;'=>'&#147;', '&rdquo;'=>'&#148;', '&bull;'=>'&#149;', '&ndash;'=>'&#150;', '&mdash;'=>'&#151;', '&tilde;'=>'&#152;', '&trade;'=>'&#153;', '&scaron;'=>'&#154;', '&rsaquo;'=>'&#155;', '&oelig;'=>'&#156;', '&Yuml;'=>'&#159;', '&yuml;'=>'&#255;', '&OElig;'=>'&#338;', '&oelig;'=>'&#339;', '&Scaron;'=>'&#352;', '&scaron;'=>'&#353;', '&Yuml;'=>'&#376;', '&fnof;'=>'&#402;', '&circ;'=>'&#710;', '&tilde;'=>'&#732;', '&Alpha;'=>'&#913;', '&Beta;'=>'&#914;', '&Gamma;'=>'&#915;', '&Delta;'=>'&#916;', '&Epsilon;'=>'&#917;', '&Zeta;'=>'&#918;', '&Eta;'=>'&#919;', '&Theta;'=>'&#920;', '&Iota;'=>'&#921;', '&Kappa;'=>'&#922;', '&Lambda;'=>'&#923;', '&Mu;'=>'&#924;', '&Nu;'=>'&#925;', '&Xi;'=>'&#926;', '&Omicron;'=>'&#927;', '&Pi;'=>'&#928;', '&Rho;'=>'&#929;', '&Sigma;'=>'&#931;', '&Tau;'=>'&#932;', '&Upsilon;'=>'&#933;', '&Phi;'=>'&#934;', '&Chi;'=>'&#935;', '&Psi;'=>'&#936;', '&Omega;'=>'&#937;', '&alpha;'=>'&#945;', '&beta;'=>'&#946;', '&gamma;'=>'&#947;', '&delta;'=>'&#948;', '&epsilon;'=>'&#949;', '&zeta;'=>'&#950;', '&eta;'=>'&#951;', '&theta;'=>'&#952;', '&iota;'=>'&#953;', '&kappa;'=>'&#954;', '&lambda;'=>'&#955;', '&mu;'=>'&#956;', '&nu;'=>'&#957;', '&xi;'=>'&#958;', '&omicron;'=>'&#959;', '&pi;'=>'&#960;', '&rho;'=>'&#961;', '&sigmaf;'=>'&#962;', '&sigma;'=>'&#963;', '&tau;'=>'&#964;', '&upsilon;'=>'&#965;', '&phi;'=>'&#966;', '&chi;'=>'&#967;', '&psi;'=>'&#968;', '&omega;'=>'&#969;', '&thetasym;'=>'&#977;', '&upsih;'=>'&#978;', '&piv;'=>'&#982;', '&ensp;'=>'&#8194;', '&emsp;'=>'&#8195;', '&thinsp;'=>'&#8201;', '&zwnj;'=>'&#8204;', '&zwj;'=>'&#8205;', '&lrm;'=>'&#8206;', '&rlm;'=>'&#8207;', '&ndash;'=>'&#8211;', '&mdash;'=>'&#8212;', '&lsquo;'=>'&#8216;', '&rsquo;'=>'&#8217;', '&sbquo;'=>'&#8218;', '&ldquo;'=>'&#8220;', '&rdquo;'=>'&#8221;', '&bdquo;'=>'&#8222;', '&dagger;'=>'&#8224;', '&Dagger;'=>'&#8225;', '&bull;'=>'&#8226;', '&hellip;'=>'&#8230;', '&permil;'=>'&#8240;', '&prime;'=>'&#8242;', '&Prime;'=>'&#8243;', '&lsaquo;'=>'&#8249;', '&rsaquo;'=>'&#8250;', '&oline;'=>'&#8254;', '&frasl;'=>'&#8260;', '&euro;'=>'&#8364;'
chris 21-Feb-2007 05:49
and a few more :
'&image;'=>'&#8465;', '&weierp;'=>'&#8472;', '&real;'=>'&#8476;', '&trade;'=>'&#8482;', '&alefsym;'=>'&#8501;', '&larr;'=>'&#8592;', '&uarr;'=>'&#8593;', '&rarr;'=>'&#8594;', '&darr;'=>'&#8595;', '&harr;'=>'&#8596;', '&crarr;'=>'&#8629;', '&lArr;'=>'&#8656;', '&uArr;'=>'&#8657;', '&rArr;'=>'&#8658;', '&dArr;'=>'&#8659;', '&hArr;'=>'&#8660;', '&forall;'=>'&#8704;', '&part;'=>'&#8706;', '&exist;'=>'&#8707;', '&empty;'=>'&#8709;', '&nabla;'=>'&#8711;', '&isin;'=>'&#8712;', '&notin;'=>'&#8713;', '&ni;'=>'&#8715;', '&prod;'=>'&#8719;', '&sum;'=>'&#8721;', '&minus;'=>'&#8722;', '&lowast;'=>'&#8727;', '&radic;'=>'&#8730;', '&prop;'=>'&#8733;', '&infin;'=>'&#8734;', '&ang;'=>'&#8736;', '&and;'=>'&#8743;', '&or;'=>'&#8744;', '&cap;'=>'&#8745;', '&cup;'=>'&#8746;', '&int;'=>'&#8747;', '&there4;'=>'&#8756;', '&sim;'=>'&#8764;', '&cong;'=>'&#8773;', '&asymp;'=>'&#8776;', '&ne;'=>'&#8800;', '&equiv;'=>'&#8801;', '&le;'=>'&#8804;', '&ge;'=>'&#8805;', '&sub;'=>'&#8834;', '&sup;'=>'&#8835;', '&nsub;'=>'&#8836;', '&sube;'=>'&#8838;', '&supe;'=>'&#8839;', '&oplus;'=>'&#8853;', '&otimes;'=>'&#8855;', '&perp;'=>'&#8869;', '&sdot;'=>'&#8901;', '&lceil;'=>'&#8968;', '&rceil;'=>'&#8969;', '&lfloor;'=>'&#8970;', '&rfloor;'=>'&#8971;', '&lang;'=>'&#9001;', '&rang;'=>'&#9002;', '&loz;'=>'&#9674;', '&spades;'=>'&#9824;', '&clubs;'=>'&#9827;', '&hearts;'=>'&#9829;', '&diams;'=>'&#9830;'
Jérôme Jaglale 31-Dec-2006 11:43
htmlentities includes htmlspecialchars, so here's how to convert an UTF-8 string :
htmlentities($string, ENT_QUOTES, 'UTF-8');
zohar at zohararad dot com 04-Dec-2006 06:31
Another way of converting HTML entities into numeric entities to please XML parsers is using two arrays as conversion tables in a preg_replace function. The conversion table mechanism is based on Ryan's examples above.

<?php
function xmlEntities($s){
//build first an assoc. array with the entities we want to match
$table1 = get_html_translation_table(HTML_ENTITIES, ENT_QUOTES);

//now build another assoc. array with the entities we want to replace (numeric entities)
foreach ($table1 as $k=>$v){
 
$table1[$k] = "/$v/";
 
$c = htmlentities($k,ENT_QUOTES,"UTF-8");
 
$table2[$c] = "&#".ord($k).";";
}

//now perform a replacement using preg_replace
//each matched value in array 1 will be replaced with the corresponding value in array 2
$s = preg_replace($table1,$table2,$s);
return
$s;
}
?>
trukin at gmail dot com 29-Oct-2006 11: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 07: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).
rbotzer at yahoo dot com 19-Jul-2005 02:10
The existance of html entities such as &quot; inside an xml node causes most xml parsers to throw an error.  The following function cleans an input string by converting html entities to valid unicode entities.

<?php

function htmlentities2unicodeentities ($input) {
 
$htmlEntities = array_values (get_html_translation_table (HTML_ENTITIES, ENT_QUOTES));
 
$entitiesDecoded = array_keys   (get_html_translation_table (HTML_ENTITIES, ENT_QUOTES));
 
$num = count ($entitiesDecoded);
  for (
$u = 0; $u < $num; $u++) {
   
$utf8Entities[$u] = '&#'.ord($entitiesDecoded[$u]).';';
  }
  return
str_replace ($htmlEntities, $utf8Entities, $input);
}
?>

So, an input of
Copyrights &copy; make &quot;me&quot; grin &reg;

outputs
Copyrights &#169; make &#34;me&#34; grin &#174;
Patrick nospam at nospam mesopia dot com 29-May-2005 07: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 18-May-2005 04: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 26-Jan-2005 02: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);
}
pinkpanther at swissonline dot ch 28-Jul-2003 02:29
In case you want a 'htmlentities' function which prevents 'double' encoding of the ampersands of already present entities (&gt; => &amp;gt;), use this:

<?php
function htmlentities2($myHTML) {
  
$translation_table=get_html_translation_table (HTML_ENTITIES,ENT_QUOTES);
  
$translation_table[chr(38)] = '&';
   return
preg_replace("/&(?![A-Za-z]{0,4}\w{2,3};|#[0-9]{2,3};)/","&amp;" , strtr($myHTML, $translation_table));
}
?>
kevin_bro at hostedstuff dot com 03-Jan-2003 06: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);
}
kumar at chicagomodular.com 28-Oct-2002 05:51
without heavy scientific analysis, this seems to work as a quick fix to making text originating from a Microsoft Word document display as HTML:

<?php
function DoHTMLEntities ($string)
    {
       
$trans_tbl = get_html_translation_table (HTML_ENTITIES);
       
       
// MS Word strangeness..
        // smart single/ double quotes:
       
$trans_tbl[chr(145)] = '\'';
       
$trans_tbl[chr(146)] = '\'';
       
$trans_tbl[chr(147)] = '&quot;';
       
$trans_tbl[chr(148)] = '&quot;';

               
// Acute 'e'
       
$trans_tbl[chr(142)] = '&eacute;';
       
        return
strtr ($string, $trans_tbl);
    }
?>
alan at akbkhome dot com 03-Jun-2002 10: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 19-Jun-2001 01: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.

 
show source | credits | stats | sitemap | contact | advertising | mirror sites