How can a regular expression be used in PHP to extract email addresses from a webpage?

To extract email addresses from a webpage using regular expressions in PHP, you can use the preg_match_all function with a specific regex pattern that matches email addresses. The regex pattern should include the common format of an email address, such as alphanumeric characters, periods, underscores, and the "@" symbol. By using preg_match_all, you can extract all email addresses that match the pattern from the webpage content.

<?php
// Sample webpage content
$content = file_get_contents('https://www.example.com');

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

// Extract email addresses using preg_match_all
preg_match_all($pattern, $content, $matches);

// Print the extracted email addresses
foreach ($matches[0] as $email) {
    echo $email . "\n";
}
?>