Comment saveHTML de DOMDocument sans enveloppe HTML?

Je suis à la fonction ci-dessous, j'ai du mal à la sortie de la DOMDocument sans elle, ajoutant le XML, HTML, corps et p tag wrappers avant la sortie de son contenu. Le correctif proposé:

$postarray['post_content'] = $d->saveXML($d->getElementsByTagName('p')->item(0));

Ne fonctionne que lorsque le contenu n'a pas d'éléments de niveau bloc à l'intérieur. Cependant, lorsqu'il le fait, comme dans l'exemple ci-dessous avec l'élément h1, le résultat de saveXML est tronqué...

<p>Si vous aimez</p>

J'ai été rappelé à ce post comme une solution de contournement possible, mais je ne peux pas comprendre comment le mettre en œuvre dans cette solution (voir en commentaire tentatives ci-dessous).

Des suggestions?

function rseo_decorate_keyword($postarray) {
    global $post;
    $keyword = "Jasmine Tea"
    $content = "If you like <h1>jasmine tea</h1> you will really like it with Jasmine Tea flavors. This is the last ocurrence of the phrase jasmine tea within the content. If there are other instances of the keyword jasmine tea within the text what happens to jasmine tea."
    $d = new DOMDocument();
    @$d->loadHTML($content);
    $x = new DOMXpath($d);
    $count = $x->evaluate("count(//text()[contains(translate(., 'ABCDEFGHJIKLMNOPQRSTUVWXYZ', 'abcdefghjiklmnopqrstuvwxyz'), '$keyword') and (ancestor::b or ancestor::strong)])");
    if ($count > 0) return $postarray;
    $nodes = $x->query("//text()[contains(translate(., 'ABCDEFGHJIKLMNOPQRSTUVWXYZ', 'abcdefghjiklmnopqrstuvwxyz'), '$keyword') and not(ancestor::h1) and not(ancestor::h2) and not(ancestor::h3) and not(ancestor::h4) and not(ancestor::h5) and not(ancestor::h6) and not(ancestor::b) and not(ancestor::strong)]");
    if ($nodes && $nodes->length) {
        $node = $nodes->item(0);
        //Split just before the keyword
        $keynode = $node->splitText(strpos($node->textContent, $keyword));
        //Split after the keyword
        $node->nextSibling->splitText(strlen($keyword));
        //Replace keyword with <b>keyword</b>
        $replacement = $d->createElement('strong', $keynode->textContent);
        $keynode->parentNode->replaceChild($replacement, $keynode);
    }
$postarray['post_content'] = $d->saveXML($d->getElementsByTagName('p')->item(0));
// $postarray['post_content'] = $d->saveXML($d->getElementsByTagName('body')->item(1));
// $postarray['post_content'] = $d->saveXML($d->getElementsByTagName('body')->childNodes);
return $postarray;
}
InformationsquelleAutor Scott B | 2011-02-02