【发布时间】:2019-11-07 19:21:49
【问题描述】:
对于平面文件新闻列表,我将所有数据存储在 .txt 文件中
所有 .txt 文件的列表都在目录 messages 中
要获取新闻消息的所有标题(每个 txt 文件的第二行),我使用了 foreach:
foreach($pagenewsmsgs as $file){
// open and prepare newsmessage
$newsmsg = 'messages/';
$newsmsg .= $file;
$fh = fopen($newsmsg, 'r');
$txtnewsmsg1 = file_get_contents($newsmsg);
$txtnewsmsg = stripslashes($txtnewsmsg1);
// get data out of txt file
$lines = file($newsmsg, FILE_IGNORE_NEW_LINES);// filedata into an array
$news_title = $lines[1]; // news title
echo $news_title.'<br />'; // echo all the title from each .txt file
fclose($fh);
} // end foreach
这个循环呼应了所有 .txt 文件的标题。 如何仅回显前 5 个 .txt 文件的标题?
【问题讨论】:
-
设置一个计数器并在达到 5 时退出循环。