2

I am querying image using getElementsByTagName("img") and printing it using image->src , it does not work. I also tried to use image->nodeValue this to does not work.

require('simple_html_dom.php');

$dom=new DOMDocument();
$dom->loadHTML( $str);       /*$str contains html output */

$xpath=new DOMXPath($dom); 
$imgfind=$dom->getElementsByTagName('img');  /*finding elements by tag name img*/

foreach($imgfind as $im)
{
    echo $im->src;        /*this doesnt work  */   
    /*echo $im->nodeValue;  and also this doesnt work (i tried both of them             separately ,Neither of them worked)*/

    // echo "<img src=".$im->nodeValue."</img><br>"; //This also did not work
}

/*the image is encolsed within div tags.so i tried to query value of div and print but still image was not printed*/

$printimage=$xpath->query('//div[@class="abc"]'); 
foreach($printimage as $image)
{
    echo $image->src;   //still i could not accomplish my task
}
8
  • Emmm.. Could you reduce the length of that title please? Commented Apr 17, 2013 at 5:28
  • Did you try var_dump($imgfind); Commented Apr 17, 2013 at 5:29
  • 1
    use echo $im->getAttribute('src'); instead of echo $im->src; Commented Apr 17, 2013 at 5:31
  • Also, since you're using DOM, there doesn't seem to be necessary to require('simple_html_dom.php'). Commented Apr 17, 2013 at 5:46
  • @saveATcode i tried using echo $im->getAttribute('src'); it just prints src value of image and not the image.i.e.<img src='a.jpg'> it prints a.jpg as text and it does not print the actual image . Commented Apr 17, 2013 at 6:18

2 Answers 2

7

Okay, use this to display your image:

foreach($imgfind as $im)
{
  echo "<img src=".$im->getAttribute('src')."/>"; //use this instead of echo $im->src;
}

and it will surely display your image. Make sure path to the image is correct.

Sign up to request clarification or add additional context in comments.

1 Comment

,Thanks the code worked ,there is one small correction i made to ur code ,i.e,a space before ending slash in echo "<img src=".$im->getAttribute('src')." />"; rather than echo "<img src=".$im->getAttribute('src')."/>";(it considers image to be a directory without space ..otherways its perfectly fine.thanks a lot buddy.
0

Espero te sirva

  $dom = new DOMDocument();
  $filename = "https://www.amazon.com/dp/B0896WB9XD/";
  $html = file_get_contents($filename);

  @$dom->loadHTML($html);

$imgfind=$dom->getElementsByTagName('img');
foreach($imgfind as $im)
{
    $ids= $im->getAttribute('id'); 

    if ($ids == 'landingImage') {
        $im2 = $im->getAttribute('src'); 
       echo '<img src="'.$im2.'">';
    }
    else{
        
    }
}

para amazon.

2 Comments

Please read stackoverflow.com/help/how-to-answer and use english.
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.