php - 如何在php中从包含特定单词的数组中删除文件
问题描述
我在文件夹中有一些 .txt 文件messages
。每个文件有 6 行。我的文件看起来像这样:
id_20197456 // identity
Friends //category
Test // title
10 Feb 2020 22:28 // date
John // writer
Lorum ipsum.... // message
类别名称总是在第二行
我总共有 5 个文件:4 个带有 categoryFriends
和1 个带有 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
作为类别的文件吗?
解决方案
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...
}
推荐阅读
- laravel - 使用宅基地的最新版本的流明(Laravel)基准测试较少
- html - 下拉表单在boostrap 3中重叠
- javascript - 如何让 for 循环创建一个由 9 个随机 1 位数字组成的字符串?
- ckeditor - 给 CKEditor “需要编辑器插件”错误的撇号站点
- amazon-web-services - 从 AMI 恢复时状态检查失败的 WIndows EC2 实例
- excel - 在记事本++中按字符将行数据转置为列
- c# - 为什么 (int)==(float) 总是编译为 (float)==(float)
- r - 使用 grepl 对 [:alpha:]:[:punct:] 进行数据清理
- javascript - 我在表单上捕获提交按钮的代码有效,但我做得正确吗?
- php - 如何在blade.php中使用vue获取字段的旧值