6

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 Answers 3

6

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.

Sign up to request clarification or add additional context in comments.

3 Comments

That is perfect enough, all I'm doing is trying to show images where I can when people copy paste links, thanks!
I might miss something but I can't get it work : $content = 'http://www.gettyimages.com/images/marketing/frontdoorStill/PanoramicImagesRM/FD_image.jpg'; preg_match_all('/(https?:\/\/\S+\.(?:jpg|png|gif))\s+/', $content, $matches); print_r($matches); Demo : codepad.viper-7.com/bgAbt6
My code was intended to match URLs (in the format described by the OP) within a large block of text. As you discovered, it will not match a URL at the end of a string due to the trailing \s+ which requires whitespace after the URL.
5

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

?>

3 Comments

Great. Best answer.
This will only recognize <img> tags from HTML code. This does not recognize any background-images, images in an anchor href, or, as the OP requested, image URIs that occur in a plain-text string
@Philipp yes. this is only for tags not for the bg images. If you need more to do please reffer domDocument
1

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.

1 Comment

Doesnt work, as if the url would have anything else behind it.. like <br /> or anything else.. I show you: regexr.com?35lba

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.