【问题标题】:how to strip file out of an array which contains specific word, in php如何在php中从包含特定单词的数组中删除文件
【发布时间】:2020-02-21 21:43:47
【问题描述】:

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

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

类别名称总是在第2行

我总共有 5 个文件:4 类别为 Friends1 类别为 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类别的所有文件吗?

【问题讨论】:

  • 不能进行简单的字符串比较吗?
  • 我想你需要的只是改变比较。使用if(strpos(strtolower($buffer), $filterthis) === false) {,然后添加所有没有您选择的类别的文件。如果您需要这两个信息,或者使用不同的数组。

标签: php arrays foreach


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-04
    • 2020-05-11
    • 2023-03-24
    • 2019-05-18
    • 1970-01-01
    • 1970-01-01
    • 2018-10-01
    相关资源
    最近更新 更多