I have used preg match to find in a page all img tags: <(img)[^>]*?> Code (markup): 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]); } } Code (markup): Thanks
if (preg_match_all('/<img[^>]+>/isU', $html, $matches) { for($i = 0, $size = sizeof($matches[0]); $i < $size; ++$i) { if (preg_match('#http:\/\/#si', $matches[0][$i]) break; else preg_replace('#src="#si', 'src="' . $link2 . '#', $matches[0][$i]); } }
so your problem is that you use $imgurl = preg_match_all('#http:\/\/#si', $matches[0][$i], $matches); I mean you already have array $matches so you have to use $matches2 for example or use code that I posted above btw you may also use str_replace. Sometimes it is faster. and also you might be interesting in preg_replace_callback function ask any question