将“ Image”标签替换为“ a”标签PHP DOMDocument

时间:2019-02-26 06:41:32

标签: 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>

我想用我正在使用的img的{​​{1}}标签替换所有a标签。

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

3 个答案:

答案 0 :(得分:4)

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

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

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

答案 1 :(得分:2)

您可以使用 preg_replace 功能

  $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 );

答案 2 :(得分:2)

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

OR

=>将此用于动态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 );

?>