阅读PHP中的特定行?
我正在学习PHP。我想从文件中找到第五行:阅读PHP中的特定行?,php,file-io,fgets,Php,File Io,Fgets,我正在学习PHP。我想从文件中找到第五行: hello world hey world hi world goodbye cruel world hello user hey user ... 我有reader.php: <?php $handle = @fopen("intro.txt"); $count = 5; if ($handle) { while (--$count > 0) { fgets($handle); } echo fg
hello world
hey world
hi world
goodbye cruel world
hello user
hey user
...
我有reader.php
:
<?php
$handle = @fopen("intro.txt");
$count = 5;
if ($handle) {
while (--$count > 0) {
fgets($handle);
}
echo fgets($handle);
if ( !feof($handle)) {
echo "Unexpected Exception";
}
fclose($handle);
}
?>
应打开该文件,并迭代$count
以移动--$count
行,以便我们可以读取$count
-eth行。我没有要运行的调试器
但是什么都没有印出来。为什么?这一个应该像你期望的那样工作
$lines = file('intro.txt');
echo $lines[5];
或第五行:
echo $lines[4];
也许使用
文件
更简单?@u_mulder我刚刚查阅了文件
的手册:它是否像文件('intro.txt')[5]
?调试:在while循环中,我将结果保存到变量中:$line=fgets($handle);然后使用“var_dump”语句:var_dump($count,$line);看看发生了什么,确保“循环”正常运行。嗯。。。上面写着“嘿,用户”,我想是“你好,用户”,在第五行。是因为我们必须从索引中减去1,因为数组从零开始?