Jump to content

getElementsByTagName('img') INSIDE a specific DIV


sblake161189

Recommended Posts

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!

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 :-)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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  :D

Link to comment
Share on other sites

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  :D

Link to comment
Share on other sites

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".

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.