convert part of dom element to string with html tags inside of them

With php 5.3.6 or higher you can use a node in DOMDocument::saveHTML:

foreach($elements as $element){
    echo $dom->saveHTML($element);
}

Current solution:

foreach($elements as $element){
    echo $dom->saveHTML($element);
}

Old answer (php < 5.3.6):

  1. Create new instance of DomDocument
  2. Clone node (with all sub nodes) you wish to save as HTML
  3. Import cloned node to new instance of DomDocument and append it as a child
  4. Save new instance as html

So something like this:

foreach($elements as $element){
    $newdoc = new DOMDocument();
    $cloned = $element->cloneNode(TRUE);
    $newdoc->appendChild($newdoc->importNode($cloned,TRUE));
    echo $newdoc->saveHTML();
}

Tags:

Html

Php

Dom