How can the DOMDocument class in PHP be utilized to extract ids and classes from HTML content?

To extract ids and classes from HTML content using the DOMDocument class in PHP, you can load the HTML content into a DOMDocument object, then use DOMXPath to query for elements with specific attributes such as id or class.

$html = '<div id="example" class="container">Hello, World!</div>';

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

$xpath = new DOMXPath($dom);

$elements = $xpath->query('//*[@id or @class]');

foreach ($elements as $element) {
    $id = $element->getAttribute('id');
    $class = $element->getAttribute('class');
    
    echo "ID: $id, Class: $class\n";
}