I think you're using the getElementsByTagName() the wrong way. This method is used to get HTML tags, not their attributes or value of attributes.
Please check the following example:
<?php
$html = '<a id="test" href="#">Some link text here</a>';
$html .= '<a id="test2" href="#">Some link text here</a>';
$DOM = new DOMDocument();
$DOM->loadHTML($html);
$links = $DOM->getElementsByTagName('a');
foreach($links as $link) {
if( $link->getAttribute('id') == "test" ) {
echo "<h4>Element with id test Found!</h4>";
// Or do something else here when you find the element by id="test"
// As an example for what you're trying from my understanding
echo $DOM->saveHTML($link);
// This will echo the node as html back to the browser
}
}
?>
I used two <a> tags as my HTML input, and searched for the one that has an attribute of id with the value "test".