将"Image"标签替换为"a"标签 PHP 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>

这是一种情况,即当您更改文档的内容时,您的迭代(在$dom->getElementsByTagName('img')中的标签列表)会引起问题。解决此问题的方法是使用XPath创建新的节点列表(XPath查询//img表示查找任何<img>标签),然后在此上迭代...

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

您可以使用 preg_replace function

  $pattern = '(<img)';
  $replacement = '<a ';
  $pattern2 = '(src=)';
  $replacement2 = 'href= ';
  $subject = Your text
  $temp = preg_replace($pattern, $replacement, $subject, -1 );
  echo preg_replace($pattern2, $replacement2, $temp, -1 );

php的DOMDocument类擅长的问题:

=>尝试此

    <?php
    $content = '<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>';
    $dom = new DOMDocument();
    $dom->loadHTML($content);
    foreach ($dom->getElementsByTagName('img') as $img) {
     $content = preg_replace("/<img[^>]+>/i", '<a alt="" src="https://green.com/files/images/Restaurants/60%20Degrees%20South-2.jpg" style="width: 550px; height: 491px;">', $content); 
        echo $content;
    }
     $content = $dom->saveHTML();
?>

演示:-https://paiza.io/projects/f7ege34jgw1lqsgvhz01ua

=>将此用于动态IMG路径。

<?php
$content = '<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>';
$img = '(<img )';
$replace = '<a ';
echo preg_replace($img, $replace, $content, -1 );
?>

最新更新