Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the src attribute of img tags? [duplicate]

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)->getAttribute("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?

like image 694
Weblurk Avatar asked Sep 03 '25 05:09

Weblurk


1 Answers

Drop the ->item(0) part.


like image 120
Álvaro González Avatar answered Sep 05 '25 10:09

Álvaro González