What are the potential pitfalls of using regular expressions to extract specific data from a webpage in PHP?

One potential pitfall of using regular expressions to extract specific data from a webpage in PHP is that they can be brittle and prone to breaking if the webpage's structure or content changes. To solve this issue, consider using a more robust solution like a DOM parser which can handle changes in the HTML structure more gracefully.

// Using DOM parser to extract specific data from a webpage
$html = file_get_contents('https://example.com');
$dom = new DOMDocument();
@$dom->loadHTML($html);

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

foreach ($elements as $element) {
    echo $element->nodeValue;
}