Php 从HTMLDOM元素获取图像源
我正在使用Php 从HTMLDOM元素获取图像源,php,image,parsing,printing,Php,Image,Parsing,Printing,我正在使用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); $imgfin
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
}
require('simple_html_dom.php');
$dom=新的DOMDocument();
$dom->loadHTML($str)/*$str包含html输出*/
$xpath=newdomxpath($dom);
$imgfind=$dom->getElementsByTagName('img')/*按标记名img查找元素*/
foreach($imgfind作为$im)
{
echo$im->src;/*这不起作用*/
/*echo$im->nodeValue;这也不起作用(我分别尝试了这两种方法,两种方法都不起作用)*/
//echo“nodeValue.”
“;//这也不起作用
}
/*图像在div标签中被加密。所以我试图查询div和print的值,但仍然没有打印图像*/
$printimage=$xpath->query('//div[@class=“abc”]');
foreach($printimage作为$image)
{
echo$image->src;//我仍然无法完成我的任务
}
好的,用这个来显示你的图像:
foreach($imgfind as $im)
{
echo "<img src=".$im->getAttribute('src')."/>"; //use this instead of echo $im->src;
}
foreach($imgfindas$im)
{
echo“getAttribute('src')。“/>”;//使用它而不是echo$im->src;
}
它肯定会显示你的形象确保图像路径正确好的,使用此选项显示图像:
foreach($imgfind as $im)
{
echo "<img src=".$im->getAttribute('src')."/>"; //use this instead of echo $im->src;
}
foreach($imgfindas$im)
{
echo“getAttribute('src')。“/>”;//使用它而不是echo$im->src;
}
它肯定会显示您的图像。确保图像的路径正确。Emmm..您能缩短标题的长度吗?您是否尝试过
var\u dump($imgfind);
使用echo$im->getAttribute('src'))
而不是echo$im->src;
另外,由于您使用的是DOM
,似乎没有必要要求('simple_html_DOM.php')
@saveATcode我尝试使用echo$im->getAttribute('src'));它只打印图像的src值,而不是图像。也就是说,它将a.jpg打印为文本,而不打印实际图像。Emmm..请减少标题的长度好吗?您是否尝试过var\u dump($imgfind);
使用echo$im->getAttribute('src'))
而不是echo$im->src;
另外,由于您使用的是DOM
,似乎没有必要要求('simple_html_DOM.php')
@saveATcode我尝试使用echo$im->getAttribute('src'));它只打印图像的src值,而不是图像。也就是说,它将a.jpg打印为文本,而不打印实际图像。多亏了代码的工作,我对ur代码做了一个小的修改,即在echo“getAttribute('src')”/>中斜杠结束前留一个空格;与echo“getAttribute('src')。”/>“;(它认为图像是一个没有空间的目录..其他方面它很好。非常感谢朋友,感谢代码工作,我对您的代码做了一个小的修改,即,在echo“getAttribute('src')。”/>“而不是echo”getAttribute('src')。“/>”中斜杠结束前有一个空格;(它认为图像是一个没有空间的目录..其他方面都很好。非常感谢你的朋友。