从html dom元素获取图像源 [英] Get image source from html dom element

查看:406
本文介绍了从html dom元素获取图像源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 getElementsByTagName(img)查询图像并使用 image-> src 进行打印,这是行不通的。我还尝试使用 image-> nodeValue 这不起作用。

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
}


推荐答案

好的,用它来显示你的形象:

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.

这篇关于从html dom元素获取图像源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆