我正在尝试用 img 标签替换字符串中的 URL。
我可以正常工作,但是它还替换了 anchor 标记内的 URL。
如果我有:
$string = 'Hi, https://example.com/logo.png,
<a href="https://example.com/banner.png">https://example.com/banner.png</a>';
它看起来像:
Hi, <img src="https://example.com/logo.png" />,
<a href="<img src="https://example.com/banner.png" />"><img src="https://example.com/banner.png" /></a>
这就是我用来替换的:
return preg_replace_callback('/https?:\/\/(.*?)\.(jpg|png|gif)(\?\w+=\w+)?/i', function($matches){
return '<img src="'.$matches[0].'" />';
}, $string);
如何让它忽略 anchor 标记。
谢谢。
最佳答案
不要直接使用正则表达式进行 HTML 操作。相反,您可以使用 DOMDocument 来进行 DOM 操作。查找 HTML 字符串中的所有文本节点,并安全地将图像 URL 替换为图像标签。
<?php
$string = 'Hi, https://example.com/logo.png,
<a href="https://example.com/banner.png">https://example.com/banner.png</a>';
$dom = new DOMDocument();
// This loads the HTML string in a special way to handle utf and
// not append any extra HTML tags
$dom->loadHtml(mb_convert_encoding($string, 'HTML-ENTITIES', "UTF-8"), LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
// Get the text nodes
$xpath = new DOMXPath($dom);
foreach($xpath->query('//text()') as $node) {
// Replace the text nodes with the replaced HTML fragments
$replaced = preg_replace('/(https?:\/\/[^ ]+?(?:\.jpg|\.png|\.gif))/', '<img src="$1" alt="$1" />', $node->data);
$frag = $dom->createDocumentFragment();
$frag->appendXML($replaced);
$node->parentNode->replaceChild($frag, $node);
}
echo $dom->saveHtml();
输出:
<p>Hi, <img src="https://example.com/logo.png" alt="https://example.com/logo.png">,
<a href="https://example.com/banner.png"><img src="https://example.com/banner.png" alt="https://example.com/banner.png"></a></p>
如果您还想排除 anchor 之间的图像 URL,则可以使用
$xpath->query('//text()[not(ancestor::a)]');
输出:
<p>Hi, <img src="https://example.com/logo.png" alt="https://example.com/logo.png">,
<a href="https://example.com/banner.png">https://example.com/banner.png</a></p>
我在演示中使用了这个正则表达式。请根据您的目的进行修改。
preg_replace('/(https?:\/\/[^ ]+?(?:\.jpg|\.png|\.gif))/', '<img src="$1" alt="$1" />', $string);
关于php - 将图像 url 替换为 img 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30440087/