1

I have some code that takes from a txt file a list of emails and inserts them into a database, making sure not to add the email if it's already in said database. What I'm trying to do now is filter emails as they are read from the txt file and NOT insert them if they are an exact or partial match to any strings within the $filter array. In other words, if the email has 'gmail', '.ru' or 'exec' anywhere within the email address, I don't want it added.

Any help to stop the bleeding from me pounding my head against a wall would be great!

The code:

$TheFile = "emails.txt";

$handle = fopen($TheFile, 'r');

$good_count = 0;
$bad_count = 0;

$filter= array("gmail",".ru","exec");

while (!feof($handle))
{
    $Data = fgets($handle, 1024);
    $output = explode (",",$Data);

    $exist = mysql_query("SELECT * FROM table WHERE email='$output[0]'");

    if (mysql_num_rows ($exist) == 0) {
        $email = strtolower($output[0]);
        $sql = "INSERT INTO table SET email='$email'";
        mysql_query($sql);
        $good_count = $good_count + 1;
    }
    else {
        $bad_count = $bad_count + 1;
    }
}
Dr. belisarius
  • 60,527
  • 15
  • 115
  • 190
86Stang
  • 349
  • 1
  • 4
  • 16
  • See also [how do i insert multiple values in mysql and avoid duplicates](http://stackoverflow.com/q/5986137/161052) – JYelton Aug 08 '11 at 21:18
  • Also consider that you can do [`INSERT IGNORE` or `INSERT REPLACE`](http://dev.mysql.com/doc/refman/5.1/en/insert.html) where duplicate values are skipped or overwritten. – JYelton Aug 08 '11 at 21:20
  • Thanks but that's not really what I'm after. I'm already scrubbing against duplicates (might not be the most efficient mind you). I need to match each entry against the $filter array and not insert them if there is a match. – 86Stang Aug 08 '11 at 22:04
  • http://php.net/manual/en/function.in-array.php – vascowhite Aug 08 '11 at 22:10

1 Answers1

1

Use stripos in a validation function:

function validate_email($input, array $needles) {
    foreach ($needles as $needle) {
        if (stripos($input, $needle) === false) return false;
    }
    return true;
}

// ...

if (mysql_num_rows ($exist) == 0 &&
    validate_email($output[0], $filter)) 
{
    $email = strtolower($output[0]);
    $sql = "INSERT INTO table SET email='$email'";
    mysql_query($sql);
    $good_count = $good_count + 1;
}
else {
    $bad_count = $bad_count + 1;
}

Also, consider using a UNIQUE index in your table definition. This will cause a (catchable) MySQL error if the email already exists and will offload your script from doing a SELECT query for every email in your file.

netcoder
  • 66,435
  • 19
  • 125
  • 142
  • Excuse the noob stab at the logic of your answer but I really want to learn something from this and not just copy/paste an answer... In your function if there is even one match, it will return false and end the function, otherwise it will return true, correct? Assuming that's correct, the IF statement with the function is saying "if the email doesn't exist and the function returns false execute the curly braces". Is that close? – 86Stang Aug 08 '11 at 23:16
  • @86Stang: The function must return true (i.e.: none of the `$needles` are found in `$input`) for the if statement (curly braces) to be executed. BTW, don't feel bad about "noobiness" :) We all went through it at some point. I prefer you learn something than just copy/paste! Hell, if you learn something, you'll be able to answer questions on StackOverflow yourself. :D – netcoder Aug 09 '11 at 00:11