首页 > 解决方案 > 如何在php中从包含特定单词的数组中删除文件

问题描述

我在文件夹中有一些 .txt 文件messages。每个文件有 6 行。我的文件看起来像这样:

id_20197456 // identity
Friends //category
Test // title
10 Feb 2020 22:28 // date
John // writer
Lorum ipsum.... // message

类别名称总是在第二

我总共有 5 个文件:4 个带有 categoryFriends1 个带有 category Offside。现在我想删除具有类别的文件Offside

这就是我到目前为止所拥有的,以捕获具有类别的文件Offside

$filterthis = strtolower('Offside');
$newslist = array();

$files = glob("messages/*.txt"); // Specify the file directory by extension (.txt)

foreach($files as $file) { // Loop through the files in the directory   

    $handle = @fopen($file, "r");

    if ($handle) {

        $lines = file($file); //file into an array

        $buffer = $lines[1]; // grab category line

        if(strpos(strtolower($buffer), $filterthis) !== FALSE) { // strtolower; search word not case sensitive  

                $newslist[] = $file; // The filename of the match
                // below the file which has Offside category
                print_r($newslist); // outputs: Array ( [0] => messages/id_20200210222825.txt )                 
        }
        fclose($handle);
    }
}

为了输出所有文件,我使用了一个 foreach 循环:

foreach($newslist as $file) {
    $lines = file($file, FILE_IGNORE_NEW_LINES); // filedata into an array

    $file_id = $lines[0]; // file id
    $news_category = $lines[1]; //  news category
    $news_title = $lines[2]; //  news title
    $news_date = $lines[3]; // news date
    $news_author = $lines[4]; //  author name
    $news_message = $lines[5]; // news message

    fclose($fh);

    // all the echos's come here...
}

我的问题:如何过滤在 foreach 中没有Offside作为类别的文件?所以 foreach 应该输出所有文件,除了那些Offside作为类别的文件吗?

标签: phparraysforeach

解决方案


array_diff将完成这项工作

抓取文件messages夹中的所有文件:

// read all files in messages folder
$dir = 'messages/';
if ($dh = opendir($dir)) {
    while(($file = readdir($dh))!== false){
        if ($file != "." && $file != "..") { // This line strips out . & ..
            $newslist[] = $dir.$file;                       
        }       
    }
}
closedir($dh);

现在过滤哪个类别的文件越位(你已经这样做了)

// Strip file(s) with category Offside  
$strip_cat = strtolower('Offside');
$offside_array = array();

$files = glob("messages/*.txt"); // Specify the file directory by extension (.txt)

foreach($files as $file) { // Loop through the files in the directory   

    $handle = @fopen($file, "r");

    if ($handle) {

        $lines = file($file); //file into an array

        $buffer = $lines[1]; // grab category line

        if(strpos(strtolower($buffer), $strip_cat) !== FALSE) { // strtolower; search word not case sensitive   

                $offside_array[] = $file; // The filename of the match(es)

        }
        fclose($handle);
    }
}

现在比较两个数组:

// compare the arrays and strip out the files which contain cat Offside
$filtered_newslist = array_diff($newslist, $offside_array);

$filtered_newslist是您的新数组,其中包含除具有类别的文件之外的所有文件Offside

你的 foreach 循环:

foreach($filtered_newslist as $file) {
    $lines = file($file, FILE_IGNORE_NEW_LINES); // filedata into an array

    $file_id = $lines[0]; // file id
    // and so on...
}


推荐阅读