0

I am using Regexpal.com and I saw this SO Question

I am trying to NOT match a string.

I need to preg_replace all occurences of links to images with the string <<IMAGE:{link}>>. So I was thinking to use (https?:\/\/)?\S*(jpg|png|jpeg|bmp|gif) ORing with the same thing but without the preceding (https?://)

so that:

Hi there this is an image link https://facebook.com/images/2323.jpg
and this one is too mysite.org/1.png

would become that:

Hi there this is an image link
<<IMAGE:https://facebook.com/images/2323.jpg>> and this one is too
<<IMAGE:mysite.org/1.png>>
Community
  • 1
  • 1
Ted
  • 3,805
  • 14
  • 56
  • 98

1 Answers1

1

I think you want this...

$str = 'Hi there this is an image link https://facebook.com/images/2323.jpg
and this one is too mysite.org/1.png';

$regex = '/((https?:\/\/)?\S*(jpg|png|jpeg|bmp|gif))/';

$str = preg_replace($regex, '<<IMAGE:$1>>', $str);
echo $str;

Output

Hi there this is an image link <<IMAGE:https://facebook.com/images/2323.jpg>>
and this one is too <<IMAGE:mysite.org/1.png>>

Codepad

Ashwini Agarwal
  • 4,828
  • 2
  • 42
  • 59
  • This looks good, but it matches dsfhttps://facebook.com/images/2323.jpg too which is not a valid link! – Ted Feb 05 '13 at 12:47