搜索数组以进行部分(或完全)匹配

发布于 2024-11-28 16:28:13 字数 812 浏览 2 评论 0原文

我有一些代码从 txt 文件中获取电子邮件列表并将它们插入到数据库中,确保如果电子邮件已存在于所述数据库中,则不要添加该电子邮件。我现在想做的是过滤从 txt 文件读取的电子邮件,如果它们与 $filter 数组中的任何字符串完全或部分匹配,则不插入它们。换句话说,如果电子邮件地址中的任何位置有“gmail”、“.ru”或“exec”,我不希望添加它。

任何帮助我止血的方法都会很棒!

代码:

$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;
    }
}

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;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无尽的现实 2024-12-05 16:28:13

在验证函数中使用 stripos

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;
}

另外,考虑使用 UNIQUE 索引 在表定义中。如果电子邮件已经存在,这将导致(可捕获的)MySQL 错误,并且将减轻脚本对文件中每封电子邮件执行 SELECT 查询的负担。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文