What are some best practices for adapting PHP code to changes in website structure, such as when a website owner redesigns their site?

When a website owner redesigns their site, the structure of the website may change, leading to the need to adapt any PHP code that interacts with the website's elements. One best practice for adapting PHP code to changes in website structure is to use CSS classes or IDs to target specific elements on the page, rather than relying on specific HTML structure. This allows the PHP code to remain flexible and easily adaptable to changes in the website layout.

// Example of using CSS classes to target specific elements on the page
$html = file_get_contents('https://www.example.com/');
$dom = new DOMDocument();
$dom->loadHTML($html);

// Find elements with specific CSS class
$xpath = new DOMXPath($dom);
$elements = $xpath->query('//div[@class="content"]');

foreach ($elements as $element) {
    // Do something with the element
    echo $element->nodeValue;
}