note 36131 added to function.domdocument-dump-mem
| From: | ross at dev-null dot accretivetg dot com | Date: | Sat, 27 Sep 2003 02:11:24 +0000 |
| Subject: | note 36131 added to function.domdocument-dump-mem | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-57185@lists.php.net to get a copy of this message | ||
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.
----
Manual Page -- http://www.php.net/manual/en/function.domdocument-dump-mem.php
Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+36131
Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+36131&report=yes
Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+36131&report=yes