How can DomDocument/DomXpath be used to test for the presence of a specific meta tag in HTML code, such as the "description" tag?

To test for the presence of a specific meta tag like the "description" tag in HTML code using DomDocument/DomXpath in PHP, you can load the HTML code into a DomDocument object, use DomXpath to query for meta tags with the name attribute set to "description", and then check if any matching nodes are found.

<?php
$html = '<html><head><meta name="description" content="This is a description"></head></html>';

$dom = new DomDocument();
$dom->loadHTML($html);

$xpath = new DomXpath($dom);
$descriptionMetaTags = $xpath->query('//meta[@name="description"]');

if($descriptionMetaTags->length > 0) {
    echo "Description meta tag found!";
} else {
    echo "Description meta tag not found.";
}
?>