如果有文件ex(news.txt)
我需要php在这个文件中读取我的文字只是一天一个
每天php只读一行>>
我写这段代码,但它不起作用>>>任何帮助
$wordfile = "words.txt";
$open = fopen($wordfile, "r");
$read = fread($open, filesize($wordfile));
fclose($wordfile);
$array = explode("\n",$read);
$date = date("z");
while ($date++){
echo $array;
}
答案 0 :(得分:1)
$f = file("words.txt");
echo $f[date("z")];
就像你有一个366行的文件一样简单。你的代码的问题是循环永远不会完成。你可能想要while ($date--)
,然后在循环中做一些改变“当前行”的事情。
答案 1 :(得分:0)
以下代码在运行时将以行号[自2010年7月13日以来的天数]输出记录
<?php
$startDate = mktime(0,0,0,7,13,2010);
$currentDate = time();
$dateDiff = $currentDate - $startDate;
$dayOn = floor($dateDiff/(60*60*24));
$lines = file("words.txt");
echo trim($lines[$dayOn]) . "\n";
?>
答案 2 :(得分:0)
如果您使用file()
函数,将整个文件读入数组,这将是一项更容易的任务:
$wordfile = "words.txt";
$lines = file( $wordfile );
$count = count($lines);
for ($i = 0; $i < $count; $i++) {
echo 'Line ',($i + 1),': ',$lines[$i],'<br />';
}
<强>更新强>:
如果我理解了您的要求,您将拥有一个包含七行的文本文件,每周一天。所以,代码将是这样的:
$wordfile = "words.txt";
$lines = file( $wordfile );
$count = count($lines);
$day_of_week = date('z'); // 0 (for Sunday) through 6 (for Saturday)
echo $lines( $day_of_week );
如果您需要更复杂的解决方案,可以更改行$day_of_week = date('z');
以符合您的需求。阅读有关PHP的date()
函数的更多信息。