【发布时间】:2019-12-04 19:11:44
【问题描述】:
我有很多 .txt 文件,其中包含几行内容。 要获取文件夹消息中的所有 txt 文件,我使用以下代码:
// read all files in messages folder
$dir = 'messages/';
if ($dh = opendir($dir)) {
while(($file = readdir($dh))!== false){
if ($file != "." && $file != "..") { // This line strips out . & ..
$files_list[] = $file;
}
}
}
要从 txt 文件中获取第二行,我知道我可以使用这个:
$lines = file($file);
$secondline = $lines[1];
但是我怎样才能从所有 txt 文件中获取所有第二行并将其放入一个变量中,以便我可以对其进行排序?就像是。 rsort($all_secondlines_allfiles);
【问题讨论】:
-
你是指所有偶数行,还是每个文件的第 2 行?
-
我的意思是每个文件的第 2 行
-
不确定,但你可以试试
$lines = file($dir . $file);