note 84327 modified in function.htmlentities by danbrown

From: Date: Sun, 17 May 2009 16:48:58 +0000
Subject: note 84327 modified in function.htmlentities by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-154890@lists.php.net to get a copy of this message
Correction to my previous post: the set of ENTITY declarations must be inside a <!DOCTYPE element; also &nbsp; is NOT pre-defined in XML and must be left in the entity list. I also extended the list with the windows 1252 character set using a sample function borrowed from php.net user comments and extended with euro entity which we need for our app. Here is the final code that is in our production app: <?php // Generate a list of entity declarations from the HTML_ENTITIES set that PHP knows about to dump into the document function htmlentities_entities() { $output = "<!DOCTYPE html [\n"; foreach (get_html_translation_table_CP1252(HTML_ENTITIES) as $value) { $name = substr($value, 1, strlen($value) - 2); switch ($name) { // These ones we can skip because they're built into XML case 'gt': case 'lt': case 'quot': case 'apos': case 'amp': break; default: $output .= "<!ENTITY {$name} \"&{$name};\">\n"; } } $output .= "]>\n"; return($output); } // ref: http://php.net/manual/en/function.get-html-translation-table.php#76564 function get_html_translation_table_CP1252($type) { $trans = get_html_translation_table($type); $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 $trans['euro'] = '&euro;'; // euro currency symbol ksort($trans); return $trans; } ?> [EDIT BY danbrown AT php DOT net: The user's original note contained the following text: "So here's something fun: if you create an XML document in PHP and use htmlentities() to encode text data, then later want to read and parse the same document with PHP's xml_parse(), unless you include entity declarations into the generated document, the parser will stop on the unknown entities. To account for this, I created a small function to take the translation table and turn it into XML <!ENTITY> definitions. I insert this output into the XML document immediately after the <?xml?> line and the parse errors magically vanish" ] --was-- Correction to my previous post: the set of ENTITY declarations must be inside a <!DOCTYPE element; also &nbsp; is NOT pre-defined in XML and must be left in the entity list. I also extended the list with the windows 1252 character set using a sample function borrowed from php.net user comments and extended with euro entity which we need for our app. Here is the final code that is in our production app: <?php // Generate a list of entity declarations from the HTML_ENTITIES set that PHP knows about to dump into the document function htmlentities_entities() { $output = "<!DOCTYPE html [\n"; foreach (get_html_translation_table_CP1252(HTML_ENTITIES) as $value) { $name = substr($value, 1, strlen($value) - 2); switch ($name) { // These ones we can skip because they're built into XML case 'gt': case 'lt': case 'quot': case 'apos': case 'amp': break; default: $output .= "<!ENTITY {$name} \"&{$name};\">\n"; } } $output .= "]>\n"; return($output); } // ref: http://php.net/manual/en/function.get-html-translation-table.php#76564 function get_html_translation_table_CP1252($type) { $trans = get_html_translation_table($type); $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 $trans['euro'] = '&euro;'; // euro currency symbol ksort($trans); return $trans; } ?> http://php.net/manual/en/function.htmlentities.php

« previous php.notes (#154890) next »