Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Get All URL's of Images From String

I need a function or Regex string for PHP that I can pass a string through like so:

Lorem ipsum dolor sit amet, http://www.gettyimages.com/images/marketing/frontdoorStill/PanoramicImagesRM/FD_image.jpg consectetur adipiscing elit. Nullam sed diam lectus, a rutrum orci. Suspendisse potenti. Nulla facilisi. Suspendisse potenti. Ut http://www.handsonuniverse.org/get_images/images/20090802.ngc6992.HOS.jpg ullamcorper mauris sit amet elit tristique sit amet laoreet nunc condimentum. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam euismod arcu non odio http://www.prelovac.com/vladimir/wp-content/uploads/2008/03/example.jpg aliquam vestibulum. Sed eleifend tellus id augue luctus ac ultrices leo semper.

And I would would get in return get:

http://www.gettyimages.com/images/marketing/frontdoorStill/PanoramicImagesRM/FD_image.jpg http://www.handsonuniverse.org/get_images/images/20090802.ngc6992.HOS.jpg http://www.prelovac.com/vladimir/wp-content/uploads/2008/03/example.jpg

in an array. I need it to grab the URL's based on weather or not they contain regular image extensions, such as *.jpg, *.png, *.bmp, etc. Anyone know one that exists so I can avoid reinventing the wheel? Thanks!

like image 264
Brian Leishman Avatar asked Aug 03 '12 05:08

Brian Leishman


3 Answers

Well, below will work for your example:

preg_match_all('/(https?:\/\/\S+\.(?:jpg|png|gif))\s+/', $content, $matches);

Add whatever other extensions you want to capture.

Note that the above is not necessarily robust (it would not match www.blah.com/image.jpg for example). Nor would it match URLs that did not end in the extension, even if they were images (ie, http://domain.com/blah.jpg?loadsmall=true or something). There are ways to make it more smart, but it really depends on what sort of input you are expecting, as that would drive how complex your parsing needs to be.

like image 99
Lone Shepherd Avatar answered Nov 19 '22 02:11

Lone Shepherd


If you don’t want do this with regular expressions. Instead, parse the HTML.

<?php
$html='YOUR_STRING';
$dom = new domDocument; 
$dom->loadHTML($html); 
$dom->preserveWhiteSpace = false;
$images = $dom->getElementsByTagName('img');

foreach ($images as $image) 
   {   
     echo $image->getAttribute('src'); 
   }

?>
like image 22
Sajitha Rathnayake Avatar answered Nov 19 '22 02:11

Sajitha Rathnayake


Here's the regex: /(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/g

Demo: http://regexr.com?31ni5

Credits go to some random Google results.

like image 1
sascha Avatar answered Nov 19 '22 04:11

sascha