dom - How can I get an element's serialised HTML with PHP's DOMDocument? -
dom - How can I get an element's serialised HTML with PHP's DOMDocument? -
this illustration script:
$html = <<<html <div class="main"> <div class="text"> capture text 1 </div> <div class="date"> may 2010 </div> </div> <div class="main"> <div class="text"> capture 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 $tag) { print_r($tag->nodevalue."\n"); }
this out put:
capture text 1 may 2010 capture text 2 june 2010
but need output:
<div class="text"> capture text 2 </div> <div class="date"> june 2010 </div>
or atleast able in foreach loop:
$text = $tag->query('//div[@class="text"]')->nodevalue; $date = $tag->query('//div[@class="date"]')->nodevalue;
well, nodevalue
give node's value. want what's commonly called outerhtml
echo $dom->savexml($tag);
will output looking in x(ht)ml compliant way.
as of php 5.3.6 can pass node savehtml
, which wasnt possible previously:
echo $dom->savehtml($tag);
the latter obey html4 syntax. artefacto that.
php dom domdocument
Comments
Post a Comment