仅显示文本文件中的前五行 [英] Show only five first lines from text file
本文介绍了仅显示文本文件中的前五行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个代码(来自 SteAp):
I have this code (from SteAp):
<?php
$file = fopen("news/news_2013.txt", "r");
$i = 0;
while (!feof($file)) {
$posts[] = fgets($file);
}
fclose($file);
foreach ($posts as $rawPost ){
$datePart = substr( $rawPost, 0, 19 );
$newsPart = substr( $rawPost, 20, 10000 );
echo $datePart . ': ' . $newsPart . '<br />';
}
?>
我在这里使用它:http://flamencopeko.net/news.工作完美.
I use it here: http://flamencopeko.net/news. Works perfect.
我正在尝试为主页制作一个仅显示五个最新行的版本.像这样:http://flamencopeko.net/index2.php 但仅限前五名.所以 while (!feof($file)) 不适用于此目的.
I'm trying to make a version for the main page that shows only the five newst lines. Like this: http://flamencopeko.net/index2.php But only with the top five posts. So while (!feof($file)) will not work for this purpose.
建议?
推荐答案
只需使用计数器即可跳出循环
Just use a counter and break out of the loop
$i = 0;
while (!feof($file)) {
$posts[] = fgets($file);
$i++;
if ($i >= 5) break;
}
这篇关于仅显示文本文件中的前五行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文