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

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
User avatar
Mr Tech
Forum Contributor
Posts: 424
Joined: Tue Aug 10, 2004 3:08 am

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

Post 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=""?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Look around for what are often called negative assertions.
User avatar
Mr Tech
Forum Contributor
Posts: 424
Joined: Tue Aug 10, 2004 3:08 am

Post 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 ?.
Post Reply