如何在PHP中从X行到Y行打开文件? [英] How do I open a file from line X to line Y in PHP?
本文介绍了如何在PHP中从X行到Y行打开文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在PHP文档中看到的最接近的是fread()给定的长度,但这并没有指定从哪一行开始.还有其他建议吗?
The closest I've seen in the PHP docs, is to fread() a given length, but that doesnt specify which line to start from. Any other suggestions?
推荐答案
您将无法从X行开始读取,因为行可以是任意长度.因此,您必须从头开始阅读,以计数读取的行数才能到达X行.例如:
You not going to be able to read starting from line X because lines can be of arbitrary length. So you will have to read from the start counting the number of lines read to get to line X. For example:
<?php
$f = fopen('sample.txt', 'r');
$lineNo = 0;
$startLine = 3;
$endLine = 6;
while ($line = fgets($f)) {
$lineNo++;
if ($lineNo >= $startLine) {
echo $line;
}
if ($lineNo == $endLine) {
break;
}
}
fclose($f);
这篇关于如何在PHP中从X行到Y行打开文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文