开发者

how to handle DOM in PHP

My PHP code

$dom = new DOMDocument();
@$dom->loadHTML($file);

$xpath = new DOMXPath($dom);
$tags = $xpath->query('//div[@class="text"]');

foreach ($tags as $tag) {
    echo $tag->textContent;
}

What I'm trying to do here is to get the content of the div that has class 'text' but the problem when I loop and echo the results I only get the text I can't get the HTML code with images and all the HTML tags like p, br,开发者_高级运维 img... etc i tried to use $tag->nodeValue; but also nothing worked out.


Personally, I like Simple HTML Dom Parser.

include "lib.simple_html_dom.php"

$html = str_get_html($file);
foreach($html->find('div.text') as $e){
  echo $e->innertext;
}

Pretty simple, huh? It accommodates selectors like jQuery :)


What you need to do is create a temporary document, add the element to that and then use saveHTML():

foreach ($tags as $tag) {
  $doc = new DOMDocument;
  $doc->appendChild($doc->importNode($tag, true));
  $html = $doc->saveHTML();
} 


I found this snippet at http://www.php.net/manual/en/class.domelement.php:

<?php
function getInnerHTML($Node)
{
     $Body = $Node->ownerDocument->documentElement->firstChild->firstChild;
     $Document = new DOMDocument();    
     $Document->appendChild($Document->importNode($Body,true));
     return $Document->saveHTML();
}
?>

Not sure if it works though.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜