php基于getAttribute从html中删除标签

时间:2014-10-03 14:34:45

标签: php

如何通过指定$tag->getAttribute('rel') = "icon"来限制删除哪个链接标记?我尝试在$remove[] $tags as $tag;行添加一个简单的if语句...代码已经完成,但是rel="icon"行的链接根本没有删除。

因此,在此示例中,应从html中删除整个链接标记:

<link rel="icon" type="image/png" href="/images/favicon.ico" />


$html = file_get_contents($url);
$dom = new DOMDocument();
$dom->loadHTML($html);

$tags = $dom->getElementsByTagName('link');

$remove = [];
foreach($tags as $tag) {
    $remove[] = $tag;
}

foreach ($remove as $tag) {
    $tag->parentNode->removeChild($tag); 
}

更新回答: @prodigitalson提供了以下最初不起作用的内容:

$html = file_get_contents($url);
$dom = new DOMDocument();
$dom->loadHTML($html);
$finder = new DOMXpath($dom);
$tags = $finder->query('//link[@rel="icon"]');

foreach ($tags as $tag)
{
$tag->parentNode->removeChild($tag); 
}

通过添加以下行作为代码的最后一行...工作完美。

$html = $dom->saveHTML();

2 个答案:

答案 0 :(得分:1)

你可以使用xpath获取所有这些:

$html = file_get_contents($url);
$dom = new DOMDocument();
$dom->loadHTML($html);
$finder = new DOMXpath($dom);
$tags = $finder->query('//link[@rel="icon"]');
$toRemove = array();

foreach ($tags as $tag)
{
  $toRemove[] = $tag;
}

// with array walk
array_walk(function($elem) { $elem->parentNode->removeChild($elem); }, $toRemove);

// with foreach
foreach ($toRemove as $tag) {
  $tag->parentNode->removeChild($tag);
}

答案 1 :(得分:0)

您可以使用函数str_replace:

轻松使用
<?php

//$html = file_get_contents($url);

$html = '<a rel="icon" href="#">link</a>';
$html = str_replace('rel="icon"', 'rel=""', $html);

echo $html;
?>