gpt4 book ai didi

php - 用 img 标签替换图片 url

转载 作者:行者123 更新时间:2023-11-28 01:30:30 25 4
gpt4 key购买 nike

我正在尝试用 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 - 用 img 标签替换图片 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30440087/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com