How can PHP be used to extract specific content from a website loaded into a variable?

To extract specific content from a website loaded into a variable in PHP, you can use the DOMDocument class to parse the HTML content and then use XPath to target and extract the specific elements you need. This allows you to easily navigate the HTML structure and retrieve the desired content.

// Load the website content into a variable
$html = file_get_contents('https://www.example.com');

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

// Load the HTML content into the DOMDocument
$dom->loadHTML($html);

// Create a new DOMXPath object
$xpath = new DOMXPath($dom);

// Use XPath query to extract specific content, for example, extract all links
$links = $xpath->query('//a');

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