You probably should use an HTML parsing solution instead of regex, to avoid surprises with badly formatted code. Something like this:
// Some example source
$source = <<<EOS
<html><body>
Images that will have host appended:
<img src="foo.png" />
and
<img src="images/en/87a%20-zzQ.png" />
Image that will be left as is:
<img src="https://www.gravatar.com/avatar/1b1f8ad9a64564a9096056e33a4805bf?s=32&d=identicon&r=PG" />
</body></html>
EOS;
// Create a DOM document and read the HTML into it
$dom = new DOMDocument();
$dom->loadHTML($source);
// Use an XPath query to find all 'img' tags
$xPath = new DOMXPath($dom);
$images = $xPath->query('//img');
// Loop through the tags
foreach ($images as $image) {
// Grab the 'src' attribute
$src = $image->getAttribute('src');
// If the attribute does not already contain a scheme (e.g. http(s)),
// append the URL with scheme and host
if ($src && (!parse_url($src, PHP_URL_SCHEME))) {
$image->setAttribute('src', "http://mywebsite.com/page/" . $src);
}
}
// Write output
$dom->formatOutput = true;
echo $dom->saveHTML();
Output:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body>
Images that will have host appended:
<img src="http://mywebsite.com/page/foo.png">
and
<img src="http://mywebsite.com/page/images/en/87a%20-zzQ.png">
Image that will be left as is:
<img src="https://www.gravatar.com/avatar/1b1f8ad9a64564a9096056e33a4805bf?s=32&d=identicon&r=PG">
</body></html>