How can regular expressions be used to extract specific data from HTML content in PHP?

Regular expressions can be used in PHP to extract specific data from HTML content by defining patterns that match the desired data. This can be useful when parsing HTML to extract specific information such as email addresses, phone numbers, or links.

$html_content = file_get_contents('example.html');

// Define the regular expression pattern to match email addresses
$pattern = '/[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/';

// Perform the regular expression match
preg_match($pattern, $html_content, $matches);

// Extract the matched email address
$email = $matches[0];

echo "Extracted email address: " . $email;