sblake161189 Posted May 1, 2012 Share Posted May 1, 2012 Hi All, I have the following code to find all the image src's for all the images within a current page. However I would like to adapt it so it only looks within the <div id="description">. Any ideas on how to do that? I have tried Googling with not much look. <?php $url="http://www.example.com?id=1"; $html = file_get_contents($url); $doc = new DOMDocument(); @$doc->loadHTML($html); $images = $doc->getElementsByTagName('img'); foreach ($images as $image) { echo $image->getAttribute('src')."<br/>"; } ?> Cheers Guys! Quote Link to comment Share on other sites More sharing options...
Muddy_Funster Posted May 1, 2012 Share Posted May 1, 2012 can you build a getElementByID array first and then use the getElementByTagName on that array? Quote Link to comment Share on other sites More sharing options...
sblake161189 Posted May 1, 2012 Author Share Posted May 1, 2012 Thanks for your reply... I have tried that but unless I am being stupid it doesnt find the <div id="description"> <?php $url="http://www.example.com?id=1"; $html = file_get_contents($url); $doc = new DOMDocument(); @$doc->loadHTML($html); $divs = $doc->getElementByID('description'); foreach ($divs as $div) { echo "Found the description div <br />"; } ?> It finds nothing and appears blank... Quote Link to comment Share on other sites More sharing options...
Muddy_Funster Posted May 1, 2012 Share Posted May 1, 2012 it's deffinately id=description and not class=description? if there is more than one instance of it on the page it should be a class, not an id. In which case you either need to look it up by class if it is calls or your kinda snookered as multiple identical id's on the same page breaches the DOM standard and will probably not work. Quote Link to comment Share on other sites More sharing options...
sblake161189 Posted May 1, 2012 Author Share Posted May 1, 2012 Hi there, Nope its the only div with that ID as per W3C validations... Here is the code within the page its looking at... <div id="description"> <?php echo($blog['description']); ?> </div> Although its a foreach it still should find just that one instance of the <div id="description"> in there. Thanks for your help :-) Quote Link to comment Share on other sites More sharing options...
Adam Posted May 1, 2012 Share Posted May 1, 2012 getElementsByTagName() returns a node list, which is why you're able to iterate through it with a foreach. getElementById() returns a single element. You need to call getElementsByTagName() on that element to get a node list of child image elements. Quote Link to comment Share on other sites More sharing options...
silkfire Posted May 1, 2012 Share Posted May 1, 2012 Use XPath, matey=) Quote Link to comment Share on other sites More sharing options...
Adam Posted May 1, 2012 Share Posted May 1, 2012 Use XPath, matey=) I was going to suggest that actually, but I thought programming the traversal manually could be more beneficial to someone learning. Edit For reference, Xpath essentially just translates a query-like string into what you're trying to do now. Would be as simple as: //div[@id=description]//img which would return the same node list. Quote Link to comment Share on other sites More sharing options...
sblake161189 Posted May 1, 2012 Author Share Posted May 1, 2012 Hi Guys, Thanks for all your help... I have found the following works... it might not be the tidiest solution, but it does the job... <?php $url="http://www.example.com?id=1"; $html = file_get_contents($url); $doc = new DOMDocument(); @$doc->loadHTML($html); $doc = $doc->getElementById('blogdescription'); $images = $doc->getElementsByTagName('img'); foreach ($images as $image) { echo $image->getAttribute('src')."<br/>"; } ?> Cheers Quote Link to comment Share on other sites More sharing options...
sblake161189 Posted May 1, 2012 Author Share Posted May 1, 2012 Just incase any of you are confused... The reason it didnt like <div id="description"> was because when it was looking for getElementByID('description'); it was finding the META 'description' tag rather than my DIV. I have changed it in the above code to <div id="blogdescription"> as this was then different to the META. Changing that and the above code made it work Quote Link to comment Share on other sites More sharing options...
Adam Posted May 1, 2012 Share Posted May 1, 2012 Really? That shouldn't happen. What mark-up are you using for the meta tag? <?php $html = '<html> <head> <meta name="description" value="foo" /> </head> <body> <div id="description">bar</div> </body> </html>'; $dom = new DOMDocument(); $dom->loadHTML($html); $description = $dom->getElementById('description'); echo $dom->saveXML($description); This echoes "bar". 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.