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? Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted June 14, 2009 Share Posted June 14, 2009 DOM Quote Link to comment 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 ??? Quote Link to comment 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 Quote Link to comment 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? Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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> Quote Link to comment 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')); Quote Link to comment 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())); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.