note 84280 deleted from function.htmlentities by danbrown

From: Date: Sun, 17 May 2009 16:49:22 +0000
Subject: note 84280 deleted from function.htmlentities by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-154891@lists.php.net to get a copy of this message
Note Submitter: keenskelly at gmail dot com ---- 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: // Generate a list of entity declarations from the HTML_ENTITIES set that PHP knows about to dump into the document function htmlentities_entities() { $output = ''; foreach (get_html_translation_table(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 'nbsp': case 'gt': case 'lt': case 'quot': case 'apos': case 'amp': break; default: $output .= "<!ENTITY {$name} \"&{$name};\">\n"; } } return($output); }

« previous php.notes (#154891) next »