Welcome to WebmasterWorld Guest from 107.21.183.163

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Get content of div with id

Problem using DOM

     

kiwibrit

10:18 pm on Oct 20, 2011 (gmt 0)

10+ Year Member



I need to get the content of a div with id on a web page.

The code below grabs all the content (showing include call ups, not the include content). Outside the required div the code shows no line returns - inside it does show the line returns, so in that sense it does highlight the required div. The id div does have class divs nested within it - these show up correctly. How can I stop the unwanted coding outside the required div being written to the text file?


$dom = new DOMDocument();
libxml_use_internal_errors(true);
@$dom->loadHTMLFile("$url");
libxml_use_internal_errors(false);
$xpath = new DOMXpath($dom);
$divTag = $xpath->evaluate("//div[@id=intwrap]");
$divcontent = $divTag->item(0);
$content = $dom->saveXML($divcontent);
$content = str_replace("
", "\r", $content);
$file = 'test.txt';
file_put_contents ($file, $content);
header('Location: test.txt');

kiwibrit

10:16 am on Oct 21, 2011 (gmt 0)

10+ Year Member



This does give me just the text content of the div - but no tags or line breaks, which I need. Could this be developed to what I want? Right now I can't figure it.

$dom = new DOMDocument;
libxml_use_internal_errors(true);
$dom->loadHTMLFile("$url");
libxml_use_internal_errors(false);
$node = $dom->getElementById('intwrap');
echo $node->nodeValue;

kiwibrit

7:58 pm on Oct 21, 2011 (gmt 0)

10+ Year Member



Thanks to some helpful posts on phpBuilder, this one has been cracked. For the sake of anybody who looks here for assistance, this does what I want.


$dom = new DOMDocument;
libxml_use_internal_errors(true);
$dom->loadHTMLFile("$url");
libxml_use_internal_errors(false);
$node = $dom->getElementById('intwrap');
$content = $dom->saveXML($node);
$content = str_replace("
", "", $content);
$file = 'test.txt';
file_put_contents ($file, $content);
header('Location: test.txt');


Note: I am using php 5.3.5