What role do regular expressions play in filtering URLs from HTML source code in PHP?

Regular expressions play a crucial role in filtering URLs from HTML source code in PHP by allowing us to search for specific patterns that match URLs within the HTML code. By using regular expressions, we can extract URLs from the HTML source code and filter out any unwanted content.

$html = file_get_contents('https://example.com');
$pattern = '/https?:\/\/[\w\-\.]+(\.\w+)+[^\s<"]*/';
preg_match_all($pattern, $html, $matches);

$urls = $matches[0];
foreach ($urls as $url) {
    echo $url . "\n";
}