Search code examples
phphtmlxmldomspecial-characters

PHP DOMDocument does not keep numeric presentation of HTML special characters


I have a DOMDocument and would like to append some nodes.

In one of the nodes, I would like to put:

$copyrightStatementText = "© This is the CopyRight";

The problem is that the function:

$copyrightStatement = $dom_output->createElement('copyright-statement', $copyrightStatementText);

Is converting the © immediately to ©.

My goal is to keep the ©

Any idea how could I do that?


Solution

  • From DOMDocument::createElement():

    Note: The value will not be escaped. Use DOMDocument::createTextNode() to create a text node with escaping support.

    So use DOMDocument::createTextNode() instead:

    $copyrightString = "© This is the Copyright";
    $copyrightNode = $dom_output->createTextNode($copyrightString);
    $copyrightContainer = $dom_output->createElement('copyright-statement');
    
    $copyrightContainer->appendChild($copyrightNode);