0

I have nodes, and iterate them in loop.

$html = <<<HTML
    <div id="test">
    <span>1</span>
    <span>2</span>
    </div>
HTML;
$dom= new Zend_Dom_Query($html);
$results = $dom->query('span');
foreach($results as $node){
...
}

How get html code of node? (not innerHTML, full HTML code <span>1</span>)

Andrey Vorobyev
  • 896
  • 1
  • 10
  • 37

2 Answers2

2
$htmlNode = iconv('UTF-8','ISO-8859-1',$results->getDocument()->saveXML($node));

Iconv exist here because i have russian characters.

Andrey Vorobyev
  • 896
  • 1
  • 10
  • 37
2

I was recently working on Zend_Dom_Query. Was having a very hard time to figure this out. Finally got the solution. So this answer is for those still struggling out there.

$dom = new Zend_Dom_Query($html);
$results = $dom->query('div#test');
foreach($results as $node){
    if($node->hasChildnodes()) {
        $childNodes = $node->childNodes;
        $countOfNodes = $childNodes->length;
        $firstSpan = $childNodes->item(0)->C14N();
    }
}

$firstSpan will contain <span>1</span>. You can also loop through the nodes using $countOfNodes to get 2nd span or nth element

Please check PHP:DOMElement - Manual and PHP:DOMNodeList for more info.

VishwaKumar
  • 3,433
  • 8
  • 44
  • 72