从php中的文件中删除不同数量的记录

时间:2016-08-14 13:16:46

标签: php file

我有一个文本文件,其中存储了50000个电子邮件ID。我需要通过删除unsubscribes的电子邮件ID来过滤此文件中的数据。

我试过这段代码但没有运气

<?php
ini_set("memory_limit","1520M");
ini_set('display_errors',1);
error_reporting(E_ALL);
$emails = $_POST['emails'];
$data = $_POST['data'];
$path= $_SERVER['DOCUMENT_ROOT']."/data/$data";
$lines = file($path);
$remove = explode("\n",$emails);
foreach ($remove as $rm)
{
foreach($lines as $key => $line)
if(stristr($line, $rm)) unset($lines[$key]);
}
$data1 = implode('', $lines);
$file = fopen($path, "w+");
fwrite($file, $data1);
fclose($file);
?>

如何从文件中删除不同数量的电子邮件ID并将其他ID保留在文件中?

txt文件包含电子邮件ID为 shvhsad@domfvh.com hgffhefg@domnvhg.com hdewjgweh@domaingfd.com

........
........

我需要删除
来自该文件的hgffhefg@domnvhg.com hdewjgweh@domaingfd.com封电子邮件。

1 个答案:

答案 0 :(得分:0)

这是:

ini_set("memory_limit","1520M");
error_reporting(E_ALL);

// if something has been posted
if (isset($_POST['emails']))
{
    $emails_delete = preg_split('/\r\n|[\r\n]/', $_POST['emails']);
    $emails = file('emails.txt');

    foreach ($emails_delete as $delete)
    {
        $email = trim($delete);

        // check if the email is in the list
        $pos = array_search($email, $emails);
        if (false !== $pos)
        {
            unset($emails[$pos]);
        }

        // save it again
        file_put_contents('emails.txt', $emails);
    }
}
?>
<form action="" method="post">
  <textarea name="emails"></textarea><br />
  <input type="submit" value="Delete" />
</form>