Are there any best practices for using DOMDocument::createTextNode() in PHP for XML manipulation?

When using DOMDocument::createTextNode() in PHP for XML manipulation, it is important to ensure that the text content being added does not contain characters that need to be escaped in XML. This can include characters like <, >, ", ', and &. To avoid issues with invalid XML, it is recommended to use htmlspecialchars() or htmlentities() to escape special characters before creating a text node.

// Example of creating a text node with htmlspecialchars() to escape special characters
$dom = new DOMDocument();
$text = htmlspecialchars(&quot;This is some text with &lt;special&gt; characters &amp; symbols&quot;);
$textNode = $dom-&gt;createTextNode($text);

// Add the text node to an element
$element = $dom-&gt;createElement(&#039;example&#039;);
$element-&gt;appendChild($textNode);

// Output the XML
echo $dom-&gt;saveXML();