What are some potential pitfalls when trying to extract emails from a text list using PHP?

One potential pitfall when extracting emails from a text list using PHP is that the regular expression used may not capture all valid email formats, leading to some emails being missed. To address this, it's important to use a robust regular expression pattern that covers a wide range of valid email formats.

$text = "john.doe@example.com, jane.smith@example.com, invalid_email, test@test";

$emails = [];
preg_match_all('/[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/', $text, $matches);

if (!empty($matches[0])) {
    $emails = $matches[0];
}

print_r($emails);