Page 1 of 1

Replacing img tags that don't contain http://

Posted: Wed Nov 07, 2007 11:32 pm
by Mr Tech
I have the following code to replace img tags

Code: Select all

$content = preg_replace('#src="([^"]+?)#im', "src=\"".$websiteurl."/$1", $content);
What the above does is add the website url infront of the images.

That's all nice and dandy until one image tag already has a domain infront of the image src...

Is there anyway to only make the above happen if it does not detect a http:// or https:// in the src=""?

Posted: Wed Nov 07, 2007 11:34 pm
by feyd
Look around for what are often called negative assertions.

Posted: Wed Nov 07, 2007 11:51 pm
by Mr Tech
Feyd, you are a legend. What would I do without ya 8) Not much!

For those interested, here's how I did it:

Code: Select all

$content = preg_replace('#src="([^"]+?!http://|!https://)#im', "src=\"".$websiteurl."/$1", $content);
Simply added !http://|!https:// after the ?.