Last active
May 23, 2018 13:52
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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