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.
- 5 comments
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

5 Replies 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.
April 12, 2010 at 8:04 AM
did you remove the WHOLE LINE where “PREG_PATTERN_ORDER” or just the word “PREG_PATTERN_ORDER” and the comma “,” (on both lines? or just line 17)
thank you.
April 12, 2010 at 8:55 AM
I haven’t removed “PREG_PATTERN_ORDER”. What doesn’t fit in the code box is moved to the next line. As you can see the line 17 is actually made from 2 lines.
April 13, 2010 at 1:17 AM
Hi Gabriel, I tried it but it doesn’t work.. I saw that JAY has indicated the same problem and getting the code to work once he removed the certain parts.. so i was asking him which part exactly he removed to get the code working.. thank you..
May 30, 2010 at 12:46 PM
Hi,
Remove the following lines, it will work properly without any warnings and notices:
preg_match_all($image_regex, $string, $out, PREG_PATTERN_ORDER);
$img_tag_array = $out[0];
echo "<pre>"; print_r($img_tag_array); echo "</pre>";