After struggling with parsing and modifying partial HTML content for several hours, I came to this solution which does work for me and is relatively simple compared to what else I found online.
This solution fixes unwanted DOCTYPE and html, body tags as well as encoding issues.
<?php
$content = "<h1>This is a heading</h1><p>This is a paragraph</p>";
$temp_dom = new DOMDocument();
$temp_dom->loadHTML("<meta http-equiv='Content-Type' content='charset=utf-8' /><div>$content</div>");
$dom = new DOMDocument();
$first_div = $temp_dom->getElementsByTagName('div')[0];
$first_div_node = $dom->importNode($first_div, true);
$dom->appendChild($first_div_node);
$dom->getElementsByTagName('h1')[0]->setAttribute('class', 'happy');
echo substr(trim($dom->saveHtml()), 5, -6);
?>