I have used preg match to find in a page all img tags:

<(img)[^>]*?>

Can someone please tell me how i would check if it has a full url and if it does not replace it with a full url from a variable called $link2. I would also like to be able to process urls which have change directory such as: ../images/example.gif --> to http://example.com/images/example.gif using the http://example.com previously stored in $link2.

I have tried, without any luck:

if ($urlres = true) {
						
						for($i = 0, $size = sizeof($matches[0]); $i < $size; ++$i)
					{
					$imgurl =  preg_match_all('#http:\/\/#si', $matches[0][$i], $matches);
					if ($imgurl = true) {break;} else 
					preg_replace('#src="#si', '#src="$link2#', $matches[0][$i]);
					}
			}

Thanks

Recommended Answers

All 2 Replies

I don't know about the other stuff but I know for a fact your second code didn't work be cause if ($urlres = true) { should be if ($urlres == true) { instead.

Oh, and why are you using #?

here is something I found for you, It's a start but not complete:

$site = "http://example.com/images/";

$image = preg_replace('/<img src='(.*)([\/])(.*)' \/>/i', '".$site."$3', '<img src='../../../pic.gif' />');

//$image will return http://example.com/images/pic.gif

$site has to be image folder with trailing slash or you can embed it to the reg expression right before $3

I tried this to test it:
http://www.spaweditor.com/scripts/regex/index.php

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.