Skip to content

Instantly share code, notes, and snippets.

@danvaly
Last active May 23, 2018 13:52
Show Gist options
  • Save danvaly/ea705c56ae5bd0524193099b1c81cdec to your computer and use it in GitHub Desktop.
Save danvaly/ea705c56ae5bd0524193099b1c81cdec to your computer and use it in GitHub Desktop.
Tools to extract unique emails from text files. Ex: php extract-emails.php -f text.txt -o emails.txt
<?php
/* Tools to process end extract log data from text files
* Ex: php extract-emails.php -f text.txt -o emails.txt
*/
function extract_emails ($from_text = "")
{
$emails = [];
if(!empty($from_text)) {
$res = preg_match_all(
"/[a-z0-9]+([_\\.-][a-z0-9]+)*@([a-z0-9]+([\.-][a-z0-9]+)*)+\\.[a-z]{2,}/i",
$from_text,
$matches
);
if($res) {
foreach(array_unique($matches[0]) as $email) {
$emails[] = strtolower($email);
}
}
}
return $emails;
}
$options = getopt("f:o:");
if(!isset($options['f']) || !file_exists($options['f'])) {
exit("Error: Cannot open file!");
}
if(!isset($options['o'])) {
exit("Error: Cannot create output file!");
}
// Extract data
$file_content = @file_get_contents($options['f']);
if(empty($file_content)) {
exit("Error: No email addresses found!");
}
$emails = extract_emails($file_content);
if(!$emails) {
exit("Error: No email addresses found!");
}
@file_put_contents($options['o'], implode("\r\n", $emails));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment