php基于getAttribute从html中删除标签

How can I limit which link tag is removed by specifying $tag->getAttribute('rel') = "icon"? I tried adding a simple if statement to the $remove[] $tags as $tag; line...code ran through, but the link with rel="icon" line was not at all removed.

So in this example the whole link tag should be removed from the 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); 
}

UPDATE Answer here: @prodigitalson provided the following which initially did not work:

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

by adding the following line as the last line of the code...worked perfect.

$html = $dom->saveHTML();

You can get these all with an 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);
}

You can use easy way with function str_replace:

<?php

//$html = file_get_contents($url);

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

echo $html;
?>