php - 将 "Image"标签替换为 "a"标签 PHP DOMDocument

标签 php html domdocument

我有一些像这样的 HTML 内容

<p><b style="margin: 0px; padding: 0px; border: 0px; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-stretch: inherit; line-height: inherit; font-family: Lato, Arial, Helvetica,
 sans-serif; vertical-align: baseline; color: rgb(89, 89, 89);">'60 Degrees South Bar and Grill'</b><span style="color: rgb(89, 89, 89); font-family: Lato, Arial, Helvetica, sans-serif;">&nbsp;
is an imaginative and quirky space that allows diners to enjoy the sea breeze and spectacular views of the Indian Ocean from three terraces. The bar and Grill is ideally situated in&nbsp;</span>
<b style="margin: 0px; padding: 0px; border: 0px; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-stretch: inherit; line-height: inherit; font-family: Lato, Arial, Helvetica,
 sans-serif; vertical-align: baseline; color: rgb(89, 89, 89);">Stone Town</b><span style="color: rgb(89, 89, 89); font-family: Lato, Arial, Helvetica, sans-serif;">&nbsp;on
 the Shangani strip, perfect for a 'sundowner' while watching a breathtaking sunset. Choose from a glass of wine from their international selection or a&nbsp;</span>
<p><span style="color: rgb(89, 89, 89); font-family: Lato, Arial, Helvetica, sans-serif;">
<img alt="" src="https://green.com/files/images/Restaurants/60%20Degrees%20South-2.jpg" style="width: 550px; height: 491px;"></span></p>
<p><span style="color: rgb(89, 89, 89); font-family: Lato, Arial, Helvetica, sans-serif;">
<img alt="" src="https://green.com/files/images/Restaurants/60%20Degrees%20South-2.jpg" style="width: 550px; height: 491px;"></span>
</p>

我想用 a 标签替换所有 img 标签,我正在使用 PHP DOMDocument

$dom = new DOMDocument();
$dom->loadHTML($content);
foreach ($dom->getElementsByTagName('img') as $img) {
    $src = urldecode($img->getAttribute('src'));
    if (!empty($src)) {           
        $link = $dom->createElement('a', "Image");          
        $link->setAttribute('target', '_blank');
        $link->setAttribute('href', $src);
        $img->parentNode->replaceChild($link, $img);
    }
}
$dom->saveHTML();

这段代码只会替换第一个img。我如何将所有 img 替换为 a 标签。

这是我得到的输出

<p><b style="margin: 0px; padding: 0px; border: 0px; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-stretch: inherit; line-height: inherit; font-family: Lato, Arial, Helvetica,
 sans-serif; vertical-align: baseline; color: rgb(89, 89, 89);">'60 Degrees South Bar and Grill'</b><span style="color: rgb(89, 89, 89); font-family: Lato, Arial, Helvetica, sans-serif;">&nbsp;
is an imaginative and quirky space that allows diners to enjoy the sea breeze and spectacular views of the Indian Ocean from three terraces. The bar and Grill is ideally situated in&nbsp;</span>
<b style="margin: 0px; padding: 0px; border: 0px; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-stretch: inherit; line-height: inherit; font-family: Lato, Arial, Helvetica,
 sans-serif; vertical-align: baseline; color: rgb(89, 89, 89);">Stone Town</b><span style="color: rgb(89, 89, 89); font-family: Lato, Arial, Helvetica, sans-serif;">&nbsp;on
 the Shangani strip, perfect for a 'sundowner' while watching a breathtaking sunset. Choose from a glass of wine from their international selection or a&nbsp;</span>
<p><span style="color: rgb(89, 89, 89); font-family: Lato, Arial, Helvetica, sans-serif;">
<a target="_blank" href="https://green.com/files/images/Restaurants/60%20Degrees%20South-1.jpg">Image</a></span></p>
<p><span style="color: rgb(89, 89, 89); font-family: Lato, Arial, Helvetica, sans-serif;">
<img alt="" src="https://green.com/files/images/Restaurants/60%20Degrees%20South-2.jpg" style="width: 550px; height: 491px;"></span>
</p>

最佳答案

在这种情况下,当您更改文档的内容并迭代 a (您在 $dom->getElementsByTagName('img') 中的标签列表)时,它会导致问题。解决这个问题的方法是使用 XPath 创建一个新的节点列表(XPath 查询 //img 意味着找到任何 <img> 标记),然后迭代这个...

$dom = new DOMDocument();
$dom->loadHTML($content);

$xp = new DOMXPath($dom);
foreach ($xp->query("//img") as $img) {

关于php - 将 "Image"标签替换为 "a"标签 PHP DOMDocument,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54879753/

相关文章:

javascript - jQuery - 使用渐变动画将列表项附加到现有列表

php - 有没有办法在使用 DomDocument 解析 html 时保持实体完整?

php - 服务器不会创建 DOMDocument 实例

PHP/mySQL - 在关联数组中选择 WHERE 多个值

php - 为什么我在使用具有常量值的 bindParam 时出现无法通过引用传递参数 2 错误?

javascript - 从服务器端的 POST 方法访问类型 ="file"输入

javascript - 标准化球之间的距离(碰撞后) - javascript

javascript - 单击按钮时取消选中复选框

php - PHP的Dom Node查找href属性问题

php - 配置 phabricator 和 nginx 的问题