Jump to content

looking for emails in unknown file type


ericsante

Recommended Posts

I have a huge file with a bunch of data, the delimiters are all messed up, I want to extract the email addresses from the file.  this is what I have come up with so far.  The problem is that I am not getting the email addresses, I think I messed up something in the fgetcsv.

 


$file_handle = @fopen ("messedupfile.csv");

if ($file_handle) {
while (!feof($file_handle)) {$data   = fgetcsv($file_handle, 1000, ' ', '"');
      for ($j = 0; $j < count(!feof($file_handle)); $j++) if (eregi("^[_A-Za-z0-9-]+(\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\.[A-Za-z0-9-]+)*(\.[A-Za-z]{2,3})$", $data[$j]))
	$query  = "INSERT tblEmail VALUES ";
	$query .= "('".$data[$j]."'),";
	$query2 = substr($query, 0, -1);
	mysqli_query($link, $query2);
}
fclose($file_handle);
}

 

 

Link to comment
https://forums.phpfreaks.com/topic/42790-looking-for-emails-in-unknown-file-type/
Share on other sites

I'd recommend using preg. You've got your's anchored to the beginning and end of the line for some reason. I'm guessing this file has other things in it too? I think you can simplify that regex a little:

preg_match_all('/[-A-Za-z0-9.]+@[-A-Za-z0-9]+\.[a-z]{2,3}/', $data, $matches);

You always have to compromise between getting too much and not getting anything.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.