PHP DOMDocument不保留HTML特殊字符的数字表示
问题描述:
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?
答
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);