Possible Duplicate:
PHP URL to Link with Regex
I am trying to preg_replace all links in a textblock, so that they are replaced with the string "<< IMAGE >>"
I am trying to match all links in this text block:
"Here we can see the chupacabra trying to eat http://somesite.com/img/chupacabra.jpg.
look at the the snails somesite.com/img/snails.png"
I am using this regexp:
(https?:\/\/)?.*(jpg|png|jpeg|bmp|gif)
But I can’t get it to select only the exact strings of links.
What I need is this:
“Here we can see the chupacabra trying to eat http://somesite.com/img/chupacabra.jpg.
look at the the snails somesite.com/img/snails.png“
Testing on regexpal.com
There are two problems – first * ist greedy, so it matches way to much. The second is, .* includes spaces, which aren’t in URLs, so you dont get the right URLs.
The simpliest solution is just to use \S which match everything except whitespaces:
The better way is to use this one which use ?<= for lookbehind