How to extract images from an URL in PHP
Posted on August 30, 2008, Filled under PHP,
Bookmark it
Here’s a piece of code that extracts images from an URL. This only works if the images are displayed using the IMG tag (not CSS).
This script only shows images. It doesn’t download any. The src attribute must have a full url in order for the images to show.
<?php
/*
Credits: Bit Repository
URL: http://www.bitrepository.com/
*/
$url = 'http://www.microsoft.com/';
// Fetch page
$string = FetchPage($url);
// Regex that extracts the images (full tag)
$image_regex_src_url = '/<img[^>]*'.
'src=[\"|\'](.*)[\"|\']/Ui';
preg_match_all($image_regex, $string, $out, PREG_PATTERN_ORDER);
$img_tag_array = $out[0];
echo "<pre>"; print_r($img_tag_array); echo "</pre>";
// Regex for SRC Value
$image_regex_src_url = '/<img[^>]*'.
'src=[\"|\'](.*)[\"|\']/Ui';
preg_match_all($image_regex_src_url, $string, $out, PREG_PATTERN_ORDER);
$images_url_array = $out[1];
echo "<pre>"; print_r($images_url_array); echo "</pre>";
// Fetch Page Function
function FetchPage($path)
{
$file = fopen($path, "r");
if (!$file)
{
exit("The was a connection error!");
}
$data = '';
while (!feof($file))
{
// Extract the data from the file / url
$data .= fgets($file, 1024);
}
return $data;
}
?>
NOTE: You can use this script to extract images from local files too (not necessarily URLs).
Feel free to post any comments or suggestions regarding this script.
Do you wish to receive the latest updates as soon as they are posted? Get our RSS Feed or Subscribe to the Newsletter!
- August 30, 2008
- article by Gabriel C.
- 1 comment
Sponsors
Related Posts
-
Extract URL(s) from Link(s) with PHPat September 4, 2008 with 1 comment
-
How to extract content between two delimiters in PHPat August 29, 2008 with 16 comments
-
PHP: Extract Alphabetical Sequences from a Stringat October 5, 2008
-
PHP: Extract Alphanumeric Sequences from a Stringat October 5, 2008
-
How to extract username from an e-mail address stringat September 5, 2008 with 2 comments

One Reply to "How to extract images from an URL in PHP"
December 7, 2009 at 6:54 PM
Hi there thanks for the tutorial – only one problem though, this element on line 17
preg_match_all($image_regex, $string, $out, PREG_PATTERN_ORDER);
is not functioning properly so I removed it outright. was returning:
Warning: preg_match_all() [function.preg-match-all]: Empty regular expression in /home/example/public_html/example/test-script.php on line 17
seems to work fine without it.