How can the greediness of a regex expression affect the extraction of URLs from embed code in PHP?

The greediness of a regex expression can affect the extraction of URLs from embed code in PHP by causing the regex to match more than intended, potentially capturing incorrect or incomplete URLs. To solve this issue, the regex expression should be made non-greedy by adding a "?" after the quantifier, which will make the regex match as little as possible.

$embed_code = '<iframe src="https://www.example.com/video" width="560" height="315" frameborder="0"></iframe>';

preg_match('/src="(.*?)"/', $embed_code, $matches);

$url = $matches[1];

echo $url;