从html dom元素获取图像源

suj*_*jai 1 php printing parsing image

我正在查询使用getElementsByTagName("img")和打印图像image->src,它不起作用.我也尝试使用image->nodeValue它不起作用.

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
}
Run Code Online (Sandbox Code Playgroud)

sav*_*ode 6

好的,用它来显示你的图像:

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

它肯定会显示你的形象.确保图像的路径正确.