Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save delboy1978uk/0ba9ed1562c9bd8465dcc6828997c88e to your computer and use it in GitHub Desktop.
Save delboy1978uk/0ba9ed1562c9bd8465dcc6828997c88e to your computer and use it in GitHub Desktop.
DOM Document appending fragments
<?php
$dom = new DOMDocument();
$html = '<head><meta garbage="what"/></head>';
$head = $dom->createDocumentFragment();
$head->appendXML($html);
$html = '<body><h1>Hello</h1></body>';
$body = $dom->createDocumentFragment();
$body->appendXML($html);
$html = '<p>Paragraph</p>';
$paragraph = $dom->createDocumentFragment();
$paragraph->appendXML($html);
$html = '<hl /><p>Footer</p>';
$footer = $dom->createDocumentFragment();
$footer->appendXML($html);
$body->appendChild($paragraph);
$dom->appendChild($head);
$dom->appendChild($body);
$dom->appendChild($footer);
echo $dom->saveHTML();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment