我想从使用php的文本文件读取特定行。 这里的文本文件:PHP:从文件中读取特定行
foo
foo2
我该如何获得使用PHP的第二行的内容? 这会返回第一行:
<?php
$myFile = "4-24-11.txt";
$fh = fopen($myFile, 'r');
$theData = fgets($fh);
fclose($fh);
echo $theData;
?>
..但我需要第二行。
任何帮助,将不胜感激
我想从使用php的文本文件读取特定行。 这里的文本文件:PHP:从文件中读取特定行
foo
foo2
我该如何获得使用PHP的第二行的内容? 这会返回第一行:
<?php
$myFile = "4-24-11.txt";
$fh = fopen($myFile, 'r');
$theData = fgets($fh);
fclose($fh);
echo $theData;
?>
..但我需要第二行。
任何帮助,将不胜感激
$myFile = "4-24-11.txt";
$lines = file($myFile);//file in to an array
echo $lines[1]; //line 2
你必须循环文件,直到文件末尾。
while(!feof($file))
{
echo fgets($file). "<br />";
}
fclose($file);
不是我需要的东西,但是谢谢你的回答 – 2011-04-25 05:54:41
$myFile = "4-21-11.txt";
$fh = fopen($myFile, 'r');
while(!feof($fh))
{
$data[] = fgets($fh);
//Do whatever you want with the data in here
//This feeds the file into an array line by line
}
fclose($fh);
啊。我懂了。谢谢回答。 :) – 2011-04-25 05:55:02
顺便说一句,如果您可能正在使用任何大文件,建议不要在文件中将整个文件提供给数组,例如'file()'或'file_get_contents()'。对于小文件,它的效果很好。 – Phoenix 2011-04-25 06:32:40
您可以使用以下方法来获取所有行的文件
$handle = @fopen('test.txt', "r");
if ($handle) {
while (!feof($handle)) {
$lines[] = fgets($handle, 4096);
}
fclose($handle);
}
print_r($lines);
和$lines[1]
在你的第二个行
谢谢兄弟!我很欣赏你回答的事实。 – 2011-04-25 05:59:40
OMG我缺少7代表提出意见。这是@猛禽的& @ Tomm的评论,因为这个问题仍然显示在谷歌serps高的方式。
他完全正确。对于小文件file($file);
是完全没问题的。对于大文件,这是完全过度b/c PHP阵列吃疯狂的记忆。
我只是跑了的* .csv一个小测试,其具有〜67MB(1,000,000行)的文件大小:
$t = -microtime(1);
$file = '../data/1000k.csv';
$lines = file($file);
echo $lines[999999]
."\n".(memory_get_peak_usage(1)/1024/1024)
."\n".($t+microtime(1));
//227.5
//0.22701287269592
//Process finished with exit code 0
而且因为没有人提到它,但我给了SplFileObject
一试,这是我实际上最近才为自己发现。
$t = -microtime(1);
$file = '../data/1000k.csv';
$spl = new SplFileObject($file);
$spl->seek(999999);
echo $spl->current()
."\n".(memory_get_peak_usage(1)/1024/1024)
."\n".($t+microtime(1));
//0.5
//0.11500692367554
//Process finished with exit code 0
这是在我的Win7桌面上,所以它不具有代表性的生产环境,但仍......非常不同。
这么晚的问题很好的答案,并有帮助。 +1 – Josiah 2016-04-10 00:59:32
请记住,'SplFileObject'锁定了文件。因此,当不再需要类为null时(例如'$ spl = null;'),否则您将被拒绝对该文件执行一些其他操作 - 删除,重命名,在类之外访问等。 – Wh1T3h4Ck5 2017-10-14 16:57:29
使用stream_get_line:stream_get_line - 获取从流资源排队给定的分隔符 来源:http://php.net/manual/en/function.stream-get-line.php
如果您在Linux上使用PHP,你可以试试下面的阅读,例如第74和第159行之间的文字:
$text = shell_exec("sed -n '74,159p' path/to/file.log");
如果文件很大,此解决方案很好。
您可以尝试循环,直到您想要的行,而不是EOF,并且每次将该变量重置为行(而不是添加到该行)。在你的情况下,第二行是EOF。 (for循环可能更适合我的代码)。
这样整个文件不在内存中;缺点是需要花费时间浏览文件直到您想要的位置。
<?php
$myFile = "4-24-11.txt";
$fh = fopen($myFile, 'r');
$i = 0;
while ($i < 2)
{
$theData = fgets($fh);
$i++
}
fclose($fh);
echo $theData;
?>
我喜欢daggett answer但还有另一种解决方案,你可以得到尝试,如果你的文件是不够大。
$file = __FILE__; // Let's take the current file just as an example.
$start_line = __LINE__ -1; // The same with the line what we look for. Take the line number where $line variable is declared as the start.
$lines_to_display = 5; // The number of lines to display. Displays only the $start_line if set to 1. If $lines_to_display argument is omitted displays all lines starting from the $start_line.
echo implode('', array_slice(file($file), $start_line, lines_to_display));
我会用SplFileObject类...
$file = new SplFileObject("filename");
if (!$file->eof()) {
$file->seek($lineNumber);
$contents = $file->current(); // $contents would hold the data from line x
}
#Salute# Revoutionary真棒方法:) – 2017-01-19 04:26:11
这个问题是现在很老了,但任何人都处理非常大的文件,在这里是不涉及读取每一个解决方案前一行。这也是唯一的解决方案,在我的情况下工作约1.6亿行文件。
<?php
function rand_line($fileName) {
do{
$fileSize=filesize($fileName);
$fp = fopen($fileName, 'r');
fseek($fp, rand(0, $fileSize));
$data = fread($fp, 4096); // assumes lines are < 4096 characters
fclose($fp);
$a = explode("\n",$data);
}while(count($a)<2);
return $a[1];
}
echo rand_line("file.txt"); // change file name
?>
它可以通过打开这个文件没有读什么,然后瞬间移动指针的随机位置,从这一点读取多达4096个字符,然后抓住从数据的第一个完整产品线。
注意:应该是'$ lines [1]' – 2011-04-25 05:33:08
谢谢你!这正是我需要的!另外,感谢您的回答如此之快。 – 2011-04-25 05:53:51
如果文件大小很大,这个解决方案会很慢并占用大量内存。 – Raptor 2012-08-30 09:02:54