删除与值不匹配的行。 (PHP)

Delete lines not match with the value. (PHP)

文本文件的内容 (test.txt):

115.5.108.249 2015-03-01
118.110.6.87 2018-01-03
36.120.105.13 2018-04-06

我熟悉以下内容:

$fname = "test.txt";
$date  = date("Y-m-d");
$lines = file($fname);
foreach ($lines as $line) {
    if (!strstr($line, $date)) {
        $out .= $line;
    }
}

$f = fopen($fname, "w");
fwrite($f, $out);
fclose($f);

此命令只会删除包含当前服务器历史记录和日期($date)的行。我希望此命令反向 运行。历史记录(日期)较旧且与当前服务器日期不匹配的线路清理器(PHP 运行s 所在的服务器)。我希望你明白我的意思。请帮助我。

您只需从条件中删除 ! 即可使其反转:

foreach ($lines as $line) {
    if (strstr($line, $date)) { //change this condition
        $out .= $line;
    }
}