homepage Welcome to WebmasterWorld Guest from 54.211.73.232
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Get content of div with id
Problem using DOM
kiwibrit

10+ Year Member



 
Msg#: 4377335 posted 10:18 pm on Oct 20, 2011 (gmt 0)

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+ Year Member



 
Msg#: 4377335 posted 10:16 am on Oct 21, 2011 (gmt 0)

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

10+ Year Member



 
Msg#: 4377335 posted 7:58 pm on Oct 21, 2011 (gmt 0)

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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved