Reputation: 20223
I am using the PHP DOMDocument and at one moment I am using the createTextNode
$copyrightNode = $doc->createTextNode('©');
$copyrightContainer = $dom_output->createElement('copyright-statement');
$copyrightContainer->appendChild($copyrightNode);
In the XML that is generated some time later, I am getting:
<copyright-statement>&#x000A9;</copyright-statement>
And my goal is to have
<copyright-statement>©</copyright-statement>
Any idea on how to do that?
Thank you in advance.
Upvotes: 0
Views: 793
Reputation: 5463
When PHP outputs an XML document, any characters that cannot be represented in the specified output encoding will be replaced with numeric entities (either decimal or hexadecimal, both are equivalent):
<?php
$dom = new DOMDocument;
$node = $dom->createElement('copyright-statement', '©');
$dom->appendChild($node);
$dom->encoding = 'UTF-8';
print $dom->saveXML(); // <copyright-statement>©</copyright-statement>
$dom->encoding = 'ASCII';
print $dom->saveXML(); // <copyright-statement>©</copyright-statement>
Upvotes: 1
Reputation: 64695
The correct thing to do here is to use the createEntityReference method (e.g. createEntityReference("copy");
), and then appendChild this entity.
Example:
<?php
$copyrightNode = $doc->createEntityReference("copy");
$copyrightContainer = $dom_output->createElement('copyright-statement');
$copyrightContainer->appendChild($copyrightNode);
To create ©
you could (I believe) do:
$copyrightNode = $doc->createCDATASection("©");
$copyrightContainer = $dom_output->createElement('copyright-statement');
$copyrightContainer->appendChild($copyrightNode);
Upvotes: 0