How can the title tag and other information from a URL be extracted using DOMDocument in PHP?

To extract the title tag and other information from a URL using DOMDocument in PHP, you can load the HTML content of the URL into a DOMDocument object and then use DOMXPath to query for specific elements such as the title tag.

$url = 'https://www.example.com';
$doc = new DOMDocument();
$doc->loadHTMLFile($url);

$title = $doc->getElementsByTagName('title')->item(0)->nodeValue;
echo 'Title: ' . $title . PHP_EOL;

// Extract other information using DOMXPath
$xpath = new DOMXPath($doc);
$metaDescription = $xpath->query('//meta[@name="description"]')->item(0)->getAttribute('content');
echo 'Meta Description: ' . $metaDescription . PHP_EOL;