Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Regex to get all image URLs on the page

Tags:

regex

php

Oddly enough I haven't found anywhere that has answer this question specificly, all the other stack overflow things I've found aren't exactly right.

I have a body text I need to search through for image urls, this doesn't mean anything complex but basically things like:

http://www.google.com/logo.png

http://reddit.com/idfaiodf/test.jpg

NOT

http://reddit.com/sadfasdf/test.jpgMORECONTENTHERE

All the regex I've used will include the "MORECONTENTHERE" in the results. It's frustrating as hell. I just want the URL with nothing appended after or added on before!

Also I don't want anything that does HTML image link extracting - I'm not pulling these from HTML.

Any regex to do this?

EDIT:

So here is what I'm using as a source: http://pastebin.com/dE2s1nHz

It's HTML but I didn't want to mention that because I didn't want people to do

like image 501
Matthew 'mandatory' Bryant Avatar asked Dec 30 '25 17:12

Matthew 'mandatory' Bryant


2 Answers

https?://[^/\s]+/\S+\.(jpg|png|gif)
  1. https? is "http" or "https"
  2. :// is literal
  3. [^/\s]+ is anything but a "/" or space
  4. / is literal
  5. \S+ is anything but a space
  6. \. is "."
  7. (jpg|png|gif) is image extensions, delimited by |

Result:

enter image description here

The above is taken from RegexBuddy, used in Wine on Mac. "PCRE" is equivalent to preg_* functions. Expression should work in most regular expression flavors.

like image 105
Luke Avatar answered Jan 01 '26 07:01

Luke


This matches a string ending with a known image extension.

<?php

    $string = "Oddly enough I haven't found anywhere that has answer this question specificly, all the other stack overflow things I've found aren't exactly right.

    I have a body text I need to search through for image urls, this doesn't mean anything complex but basically things like:

        http://www.google.com/logo.png

        http://reddit.com/idfaiodf/test.jpg

    NOT

        http://reddit.com/sadfasdf/test.jpgMORECONTENTHERE
    ";

    $pattern = '~(http.*\.)(jpe?g|png|[tg]iff?|svg)~i';

    $m = preg_match_all($pattern,$string,$matches);

    print_r($matches[0]);

?>

Output

Array
(
    [0] => http://www.google.com/logo.png
    [1] => http://reddit.com/idfaiodf/test.jpg
    [2] => http://reddit.com/sadfasdf/test.jpg
)
like image 41
AbsoluteƵERØ Avatar answered Jan 01 '26 09:01

AbsoluteƵERØ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!