CancelImage Upload

How to load all the image tag sources from a website or HTML document with PHP

The easiest way to load all the image source attributes from any given HTML document with PHP is by using the DOM library. If you have not encountered DOM functions before you are in for a treat. They make accessing individual components within a HTML document very simple, often providing easy solutions where previously you might have had to string together regular expressions.

Before we can do any image source loading however we will first need to initialise the DOM library using the following lines of code. If you are familiar with classes this will not look to complex. All we are doing initialising the class DOMDocument and loading the desired websites source code with the function loadHTMLFile():

<?php

$dom = new DOMDocument;

@$dom->loadHTMLFile('http://www.example.com');

$DOMXPath = new DOMXPath($dom);

...

?>

Now comes the interesting part. First we will perform a query to load every img tag within the document. Then we will specifically load the image source attribute out of each image tag:

<?php

...

$img = $DOMXPath->query('//img');

foreach ($img as $val)
{

       $imgSrc[] = $val->getAttribute('src');

}

...

?>

Finally all that is left to do is print out the paths to each individual image. In our example we will do this by using the function print_r():

<?php

...

echo '<pre>'.print_r($imgSrc, true);

?>

And there you have it. DOM is not as mystifying as you once thought. For a complete overview you will find the complete code example below:

<?php

$dom = new DOMDocument;

@$dom->loadHTMLFile('http://www.example.com');

$DOMXPath = new DOMXPath($dom);

$img = $DOMXPath->query('//img');

foreach ($img as $val)
{

       $imgSrc[] = $val->getAttribute('src');

}

echo '<pre>'.print_r($imgSrc, true);

?>


Login
Want to leave a comment?

No problem. Just enter your email and password below.


register | home | reminder

myDesignTool Networking • www.mydesigntool.cominfo@mydesigntool.com