使用 php 搜索数据并从文本文件中删除数据


Searching for data and removing data from a text file using php

我有一个将数据列表导出到.txt文件中的程序,很多数据是不需要的。每条数据的格式如下:

PUB DATE: 03/16/2012
END DATE: 06/10/2012
PUB:  my company
ADNUM: 00237978
CLASS: 0825
AD TYPE: RE
COLUMNS: 2.00
GRAPHIC FILE: some_image.jpg
AD TEXT: Text
*** end of ad

会有 20 到 50 条这样的记录,我需要做的是在文件中搜索并删除具有以 0 开头的 CLASS 的记录。因此,如果它搜索并找到 CLASS 以零开头的广告记录,它将删除该记录中的所有内容。如果它是.xml,这将很容易,但它是一个.txt文件,所以它使事情变得困难。删除所有不良数据后,它将好数据保存在新文件中。

$keep = array();
$filePath = '/path/to/txt/file.txt';
$textData = file_get_contents($filePath);
$records = explode('*** end of ad', $textData);
foreach ($records as $record) {
    if (empty($record)) {
        continue;
    }
    if ( ! preg_match('/CLASS:'s+?0/', $record)) {
        $endDate = array();
        preg_match('/END'sDATE:'s?'d{0,2}'/'d{0,2}'/'d{0,4}/', $record, $endDate);
        if ( ! empty($endDate)) {
            $parts = explode(':', $endDate[0]);
            $dateString = trim($parts[1]);
            $date = DateTime::createFromFormat('d/m/Y', $dateString);
            $currentDate = new Date();
            $currentDate->setTime(0, 0, 0);
            if ($currentDate->format('U') > $date->format('U')) {
                continue;
            }
        }
        $keep[] = $record;
    }
}
file_put_contents($filePath, implode('*** end of ad', $keep) . '*** end of ad');
$keep = array;
foreach(explode('*** end of ad', file_get_contents($filePath) as $record):
  if(!preg_match('^CLASS:'s*0825'/, $record))
    $keep[] = $record;
endforeach;
file_put_contents($filePath, implode('*** end of ad', $keep) . '*** end of ad');