views:

41

answers:

3

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

any help !!?

A: 

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.

Alxandr
+1  A: 

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();
} 
cletus
+1  A: 

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 :)

macek