// Create a new DOMDocument object $doc = new DOMDocument(); // Create a new comment node $comment = $doc->createComment('This is a comment.'); // Add the comment to the document $doc->appendChild($comment); echo $doc->saveXML();
// Load an XML document $doc = new DOMDocument(); $doc->loadXML(''); // Create a new comment node $comment = $doc->createComment('This is a comment.'); // Get the element to insert the comment before $element = $doc->getElementsByTagName('element')->item(0); // Insert the comment before the element $element->parentNode->insertBefore($comment, $element); echo $doc->saveXML(); content
Package/library: PHP DOMDocument.content