PHP Get All URL's of Images From String

2019-02-14 18:48发布

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!

3条回答
我想做一个坏孩纸
2楼-- · 2019-02-14 18:57

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.

查看更多
Juvenile、少年°
3楼-- · 2019-02-14 18:58

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.

查看更多
Root(大扎)
4楼-- · 2019-02-14 19:00

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'); 
   }

?>
查看更多
登录 后发表回答