开发者

How do I get the src attribute of img tags?

I load the DOM by an external url as such:

$dom = new DOMDocument;
$dom->loadHTMLFile( "external_url.html" );

$arrayOfSources = array();

foreach( $dom->getElementsByTagName( "img" ) as $image )
    $arrayOfSources[] = $image->item(0)->getAttribu开发者_运维知识库te("src");

This way I want to store all the src attributes of the img tags in an array, but I keep getting the error Fatal error: Call to undefined method DOMDocument::item()

What am I missing here? How do I extract all the src attributes from the img tags in an html?


Drop the ->item(0) part.



Inside that loop, you don't need to access the element with item(0).

The iterator for that collection allows you to just do a foreach() on it and have it implicitly access each element in the DOMNodeList.


Try:

$arrayOfSources[] = $image->getAttribute("src");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜