Geek Answers Handbook
How can I get a serialized HTML element with PHP DOMDocument?
This is my example script:
$html = <<<HTML <div class="main"> <div class="text"> Capture this text 1 </div> <div class="date"> May 2010 </div> </div> <div class="main"> <div class="text"> Capture this text 2 </div> <div class="date"> June 2010 </div> </div> HTML; $dom = new DOMDocument(); $dom->loadHTML($html); $xpath = new DOMXPath($dom); $tags = $xpath->query('//div[@class="main"]'); foreach ($tags as $tag) { print_r($tag->nodeValue."\n"); } This will come out:
Capture this text 1 May 2010 Capture this text 2 June 2010 But I need a conclusion:
<div class="text"> Capture this text 2 </div> <div class="date"> June 2010 </div> Or you can do something like this in my foreach loop:
$text = $tag->query('//div[@class="text"]')->nodeValue; $date = $tag->query('//div[@class="date"]')->nodeValue; +4
2 answers
Well, nodeValue will give you the value of node. You want what is usually called outerHTML
echo $dom->saveXml($tag); will output what you are looking for in X (HT) ML mode.
Starting with PHP 5.3.6, you can also pass node to saveHtml , which was previously not possible:
echo $dom->saveHtml($tag); The latter will obey HTML4 syntax. Thanks Artefacto for this.
+6
try
$dom = new DOMDocument(); $dom->loadHTML($html); $xpath = new DOMXPath($dom); $tags = $xpath->query('//div[@class="main"]'); foreach ($tags as $tag) { $innerHTML = ''; $children = $tag->childNodes; foreach ($children as $child) { $tmp_doc = new DOMDocument(); $tmp_doc->appendChild($tmp_doc->importNode($child,true)); $innerHTML .= $tmp_doc->saveHTML(); } var_dump(trim($innerHTML)); } -Pascal MARTIN
-1
All Articles