1 回答
TA贡献1811条经验 获得超4个赞
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);
}
}
现在比较 2 个数组:
// compare the arrays and strip out the files which contain cat Offside
$filtered_newslist = array_diff($newslist, $offside_array);
$filtered_newslist 是您的新数组,其中包含除具有类别的文件之外的所有文件Offside
您的前循环:
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...
}
- 1 回答
- 0 关注
- 79 浏览
添加回答
举报