所以我有一个网站,我想在那里显示文本文件中的单词。我已经在脚本应该读取的地方完成了文本文件,但它显示了整个文本文件:S
<?
$filename = "arraytext.txt";
$arr = file($filename);
foreach($arr as $line){
print $line . "<br />";
}
?>
`
它同时显示整个文本文件。我只想让它显示1行,这是第一个单词。我做错了什么?
要逐行读取文件,必须使用fgets()函数
这里有一个例子:
$file = fopen("test.txt","r");
while(! feof($file))
{
echo fgets($file). "<br />";
}
fclose($file);
<?
$filename = "arraytext.txt";
$f = fopen($filename, 'r');
echo fgets($f);
fclose($f);
?>
要每12小时运行一次,请将此行添加到"/etc/crontab"文件中(例如):
00 11,23 * * * root php path-to-your-script/name-of-your-script.php
更新:在你发表评论后,我想我更好地理解了你的问题(对不起)
忘掉"crontab"的东西
只需:
<?
$filename = "arraytext.txt";
$arr = file($filename);
foreach ($arr as $line) {
print $line . "<br />";
sleep(60 * 60 * 12); // (60 * 60) * 12 seconds sums up to 12 hours
}
?>
不过,请注意,php在"web"模式(而不是"cli"模式)下运行有严格的执行时间限制(请参阅http://php.net/manual/en/function.set-time-limit.php)
我想你在网络界面上使用这个,因为在你的问题中你使用了html标签<br />
。。。
注意:很抱歉,为什么你想每12小时写一行??:-)