- rtrim: documentation ( source)
- version_compare: documentation ( source)
<?php
$doc = new DOMDocument('1.0', 'UTF-8');
$doc->loadHTML('<p>Трололо!</p>');
$xpath = new DOMXPath($doc);
$child = $xpath->query('//p')->item(0);
if (version_compare(PHP_VERSION, '5.3.6', '>=')) {
$html .= $child->ownerDocument->saveHTML($child);
} else {
$document = new \DOMDocument('1.0', 'UTF-8');
$document->appendChild($document->importNode($child, true));
$html .= rtrim($document->saveHTML());
}