How can PHP be used to extract specific data from HTML source code, such as text within certain HTML tags?

To extract specific data from HTML source code using PHP, you can utilize the DOMDocument class to parse the HTML and then use XPath queries to target specific elements or attributes. This allows you to easily extract text within certain HTML tags by selecting the desired elements based on their tag names, classes, IDs, or other attributes.

// HTML source code
$html = '<div class="content"><h1>Title</h1><p>Paragraph content</p></div>';

// Create a new DOMDocument
$dom = new DOMDocument();
$dom->loadHTML($html);

// Use XPath to query for specific elements
$xpath = new DOMXPath($dom);
$elements = $xpath->query("//div[@class='content']/h1");

// Extract text within the selected element
if ($elements->length > 0) {
    $text = $elements->item(0)->textContent;
    echo $text; // Output: Title
}