Parse email addresses from text file to a csv in 4 lines

$matches = array();
$log = file_get_contents('your-text-file');
$match = preg_match_all("/[a-z0-9!_{|}~-]+(?:\.[a-z0-9?^_{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/", $log, $matches, PREG_PATTERN_ORDER);
file_put_contents('out.csv', implode(PHP_EOL, array_unique($matches[0])));

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s