note 84280 added to function.htmlentities

From: Date: Mon, 07 Jul 2008 17:23:51 +0000
Subject: note 84280 added to function.htmlentities
Groups: php.notes 
Request: Send a blank email to php-notes+get-141245@lists.php.net to get a copy of this message
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); } ---- Server IP: 208.69.120.35 Probable Submitter: 12.111.93.250 ---- Manual Page -- http://www.php.net/manual/en/function.htmlentities.php Edit -- https://master.php.net/note/edit/84280 Del: integrated -- https://master.php.net/note/delete/84280/integrated Del: useless -- https://master.php.net/note/delete/84280/useless Del: bad code -- https://master.php.net/note/delete/84280/bad+code Del: spam -- https://master.php.net/note/delete/84280/spam Del: non-english -- https://master.php.net/note/delete/84280/non-english Del: in docs -- https://master.php.net/note/delete/84280/in+docs Del: other reasons-- https://master.php.net/note/delete/84280 Reject -- https://master.php.net/note/reject/84280 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#141245) next »