How can DOMDocument be utilized for parsing HTML/XML content in PHP?

To parse HTML/XML content in PHP, you can utilize the DOMDocument class, which provides a convenient way to navigate and manipulate HTML/XML documents. You can load the HTML/XML content into a DOMDocument object, traverse the document using DOM methods, and extract the desired information.

// Create a new DOMDocument object
$doc = new DOMDocument();

// Load HTML content from a file
$doc->loadHTMLFile('example.html');

// Get all <a> tags in the document
$links = $doc->getElementsByTagName('a');

// Loop through the links and output their href attribute
foreach ($links as $link) {
    echo $link->getAttribute('href') . "\n";
}