Some people will complain that DOMDocument is more verbose for parsing HTML then a regex. But verbosity is okay if it means using the right tool for the job.
$previous_value = libxml_use_internal_errors(TRUE);
$string = '<p>hi, mom</p><p>bye, mom</p>';
$dom = new DOMDocument();
$dom->loadHTML($string);
$paragraphs = $dom->getElementsByTagName('p');
$last_p = $paragraphs->item($paragraphs->length - 1);
$last_p->setAttribute("class", "last");
$new_string = preg_replace('/^<!DOCTYPE.+?>/', '', str_replace( array('<html>', '</html>', '<body>', '</body>'), array('', '', '', ''), $dom->saveHTML()));
libxml_clear_errors();
libxml_use_internal_errors($previous_value);
echo htmlentities($new_string);
// <p>hi, mom</p><p class="last">bye, mom</p>