Skip to content

Instantly share code, notes, and snippets.

@eteubert
Created August 11, 2020 14:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eteubert/9979d75037b2dbd27d7a2008cb6c5aa9 to your computer and use it in GitHub Desktop.
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.
<?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