note 36131 deleted from function.domdocument-dump-mem by sobak

From: Date: Tue, 22 Nov 2016 03:40:59 +0000
Subject: note 36131 deleted from function.domdocument-dump-mem by sobak
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-207404@lists.php.net to get a copy of this message
Note Submitter: ross at dev-null dot accretivetg dot com ---- Given the missing description of the encoding parameter, I had hoped it would automatically encode the document if I set the encoding type; this is not the case. At least in PHP v4.3.3, it seems that setting the encoding type does NOTHING other than set the name displayed in the string: <?xml version="1.0" encoding="UTF-8"?> Thus if you want your values to be encoded, you need to manually encode them when you create your DOM object. E.g. assume you have a simple setup like this: <?php $text = '¡Spanish exclamation!' ; $xml = domxml_new_doc( '1.0' ); $node = $xml->create_element( 'example' ); $cdata = $xml->create_cdata_section( $text ); $node->append_child( $cdata ); $xml->append_child( $node ); echo $xml->dump_mem( true, 'UTF-8' ) ; ?> This will output the following: <?xml version="1.0" encoding="UTF-8"?> <example> <![CDATA[¡Spanish exclamation!]]> </example> However, that is not what was intended, as the exclamation is not UTF-8-encoded. To make $text be encoded correctly, you have to explicitly do so, as in: <?php $cdata = $xml->create_cdata_section( utf8_encode( $text ) ); ?> This results in the output being correctly UTF-8-encoded, as follows: <![CDATA[¡Spanish exclamation!]]> Apparently MSIE's MSXML2.DOMDocument ActiveX control requires UTF-8 encoding for special characters, and it will invalidate the entire document if those chars are not appropriately encoded, so beware.

« previous php.notes (#207404) next »