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
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.