Created
August 11, 2020 14:59
-
-
Save eteubert/9979d75037b2dbd27d7a2008cb6c5aa9 to your computer and use it in GitHub Desktop.
Generate a single XML element (for example for an RSS feed). Much better than doing it by hand as it ensures the text content is valid / escaped.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$doc = new DOMDocument(); | |
$node = $doc->createElement("itunes:summary"); | |
// either one is fine, both generate valid XML | |
// $text = $doc->createTextNode('I am <foo> example & so'); | |
$text = $doc->createCDATASection('I am <foo> example & so'); | |
$node->appendChild($text); | |
$doc->saveXML($node); | |
// => "<itunes:summary><![CDATA[I am <foo> example & so]]></itunes:summary>" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment