【发布时间】:2011-09-20 10:49:53
【问题描述】:
我通过外部 url 加载 DOM:
$dom = new DOMDocument;
$dom->loadHTMLFile( "external_url.html" );
$arrayOfSources = array();
foreach( $dom->getElementsByTagName( "img" ) as $image )
$arrayOfSources[] = $image->item(0)->getAttribute("src");
这样我想将img标签的所有src属性存储在一个数组中,但我一直收到错误Fatal error: Call to undefined method DOMDocument::item()
我在这里缺少什么?如何从 html 中的 img 标签中提取所有 src 属性?
【问题讨论】:
-
应该是
$arrayOfSources[] = $image->item[0]->getAttribute("src");注意方括号 -
@Kumary 我不认为这是正确的。
$image是一个对象,而不是一个数组。 -
@alex,即使在那种情况下,对象中也没有名为
item()的方法,AFAIK,他正在使用的对象是由其他对象组成的,他需要取消image(0)
标签: php dom domdocument