我必须在html消息中搜索图像标签,然后将网站url附加到使用正则表达式找到的任何图像url标签上
例如,如果html消息中的图像src是
/images/my_image.jpg
我需要附加url并使其看起来像这样:
http://mywebsite.com/page/images/my_image.jpg
您可能应该使用HTML解析解决方案而不是regex,以避免出现格式错误的代码。类似这样的东西:
// 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();
输出:
<!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>
您可以使用以下模式:
<?php
$pattern = "/('/images'/['w'd_]+'.jpg)'1*/ims";
$string = "bla bla bla /images/my_image.jpg," .
"bla bla lba /images/mfsafas.jpg bla bla bla /images/my_fsa.jpg";
preg_match_all($pattern, $string, $matches);
foreach($matches[0] as $match) {
$urls[] = "http://mywebsite.com/page" . $match;
}