Michdd Posted June 14, 2009 Share Posted June 14, 2009 I'm using file_get_contents on a file, I need to be able to get only the contents of a certain div named center. So I need to get <div id="center"> (this stuff) </div>. How can I get only that? Link to comment https://forums.phpfreaks.com/topic/162145-solved-getting-the-contents-of-a-certain-div/ Share on other sites More sharing options...
Daniel0 Posted June 14, 2009 Share Posted June 14, 2009 DOM Link to comment https://forums.phpfreaks.com/topic/162145-solved-getting-the-contents-of-a-certain-div/#findComment-855617 Share on other sites More sharing options...
Michdd Posted June 14, 2009 Author Share Posted June 14, 2009 Can't this be done simply with regex ??? Link to comment https://forums.phpfreaks.com/topic/162145-solved-getting-the-contents-of-a-certain-div/#findComment-855620 Share on other sites More sharing options...
N-Bomb(Nerd) Posted June 14, 2009 Share Posted June 14, 2009 Perhaps this is what you're searching for: /<div id="center">([^<]+)<\/div>/i Link to comment https://forums.phpfreaks.com/topic/162145-solved-getting-the-contents-of-a-certain-div/#findComment-855623 Share on other sites More sharing options...
Michdd Posted June 14, 2009 Author Share Posted June 14, 2009 After reading a little more about the DOM class it seems like it might be the better method. It looks a bit like javascript. 'getElementById'. However how can I get the contents of that div?: $doc = new DomDocument; $doc->validateOnParse = true; $doc->Load($_GET['page'] . '.php'); echo $doc->getElementById('center'); Of course that won't work, but is there another pointer to get the contents of that div? Link to comment https://forums.phpfreaks.com/topic/162145-solved-getting-the-contents-of-a-certain-div/#findComment-855624 Share on other sites More sharing options...
.josh Posted June 14, 2009 Share Posted June 14, 2009 regex is not really good for that sort of thing. It does not handle nested tags very well. Stick with DOM. Link to comment https://forums.phpfreaks.com/topic/162145-solved-getting-the-contents-of-a-certain-div/#findComment-855625 Share on other sites More sharing options...
Michdd Posted June 14, 2009 Author Share Posted June 14, 2009 regex is not really good for that sort of thing. It does not handle nested tags very well. Stick with DOM. Okay, then can you answer my above question? Can I get the html contained in an element from getElementById? In Javascript it would be .innerHTML, but I can't find the PHP equiv. Link to comment https://forums.phpfreaks.com/topic/162145-solved-getting-the-contents-of-a-certain-div/#findComment-855626 Share on other sites More sharing options...
Daniel0 Posted June 14, 2009 Share Posted June 14, 2009 <?php function getInnerHTML(DOMElement $node) { $body = $node->ownerDocument->documentElement->firstChild->firstChild; $document = new DOMDocument(); $document->appendChild($document->importNode($body,true)); return $document->saveHTML(); } $doc = new DOMDocument(); $doc->LoadHTMLFile('http://www.phpfreaks.com/'); echo getInnerHTML($doc->getElementById('header')); Output: <title>PHP Freaks - Index</title> Link to comment https://forums.phpfreaks.com/topic/162145-solved-getting-the-contents-of-a-certain-div/#findComment-855629 Share on other sites More sharing options...
Michdd Posted June 14, 2009 Author Share Posted June 14, 2009 That seems to be only outputting: <meta http-equiv="Content-Type" content="text/html;charset=utf-8"> (which isn't even within that div) When I'm trying to get the contents of the <div id="center"> on page 'about.php' using: <?php function getInnerHTML(DOMElement $node) { $body = $node->ownerDocument->documentElement->firstChild->firstChild; $document = new DOMDocument(); $document->appendChild($document->importNode($body,true)); return $document->saveHTML(); } $doc = new DOMDocument(); $doc->LoadHTMLFile('about.php'); echo getInnerHTML($doc->getElementById('center')); Link to comment https://forums.phpfreaks.com/topic/162145-solved-getting-the-contents-of-a-certain-div/#findComment-855632 Share on other sites More sharing options...
Daniel0 Posted June 14, 2009 Share Posted June 14, 2009 Sorry, something like this: $doc = new DOMDocument(); $doc->LoadHTMLFile('http://www.phpfreaks.com/'); $d = new DOMDocument(); $d->appendChild($d->importNode($doc->getElementById('header'), true)); $html = trim(preg_replace('#(^<[^>]+>|</[^>]+>$)#', '', $d->saveHTML())); Link to comment https://forums.phpfreaks.com/topic/162145-solved-getting-the-contents-of-a-certain-div/#findComment-855634 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.