我有一些这样的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;">
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 </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;"> 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 </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
答案 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;">
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 </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;"> 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 </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;">
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 </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;"> 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 </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 );
?>