Skip to content

Instantly share code, notes, and snippets.

@agarzon
Created July 16, 2012 14:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agarzon/3123118 to your computer and use it in GitHub Desktop.
Save agarzon/3123118 to your computer and use it in GitHub Desktop.
Extract all email address from any text content removing duplicated, and exporting as TEXT, SQL or CSV
<?php
/**
*
* Extract all email address from any text content removing duplicated
* Return string formated as: TEXT, SQL or CSV
* @author Alexander Garzon
*
*/
class emailXtractor {
function __construct($file, $format = 'txt', $tablename = 'emailmass'){
$clean = $this->removeDuplicated($this->extract(file_get_contents($file)));
header('Content-Type: text/plain');
switch ($format){
case 'txt' : echo $this->exportTxt($clean); break;
case 'sql' : echo $this->exportSql($clean, $tablename); break;
case 'csv' : echo $this->exportcsv($clean); break;
}
}
protected function extract($content) {
$regexp = '/([a-z0-9_\.\-])+\@(([a-z0-9\-])+\.)+([a-z0-9]{2,4})+/i';
preg_match_all($regexp, $content, $m);
return isset($m[0]) ? $m[0] : array ();
}
protected function removeDuplicated($array){
return array_map('strtolower', array_unique($array));
}
protected function exportCsv($array){
foreach($array as $value){
$csv .= '"null","'. $value . '","null"' . PHP_EOL;
}
return $csv;
}
protected function exportSql($array, $tablename){
$sql = '';
foreach($array as $value){
$sql .= "INSERT IGNORE INTO $tablename VALUES (NULL, '$value', '0');" . PHP_EOL;
}
return $sql;
}
protected function exportTxt($array){
foreach($array as $value){
$txt .= $value . PHP_EOL;
}
return $txt;
}
}
new emailXtractor('emailmass2.xml', 'sql', 'emailmass3');
@xeoncross
Copy link

if (($handle = fopen( $file, 'r')) !== false){
    return fread($handle, filesize($file));
}

can be replaced with

file_get_contents($file);

However, I would rather see you use the file socket handles to read in chunks at a time.

@agarzon
Copy link
Author

agarzon commented Jul 16, 2012

You're right. Changing ;)

@ashish10240
Copy link

ashish10240 commented Jan 18, 2019

Bro but how do I convert his youtube id to
Gmail .

There is all the details but no gmail

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment