我正在尝试使用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
omg我还差7个声望才能评论。这是@Raptor和@Tomm的评论,因为这个问题在谷歌搜索结果中仍然排名很高。
他完全正确。对于小文件,file($file);
非常好用。但对于大文件来说,它太过浪费资源了,因为PHP数组会消耗大量内存。
我刚刚进行了一个小测试,使用大小约为67MB(1,000,000行)的*.csv文件:
$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桌面上,所以不代表生产环境,但仍然...相差甚远。
SplFileObject
会锁定文件。因此,当不再需要该类时,请将其设置为null(例如 $spl = null;
),否则您将被拒绝对该文件执行其他操作 - 如删除、重命名、在类之外访问等。 - Wh1T3h4Ck5如果你想以那种方式去做...
$line = 0;
while (($buffer = fgets($fh)) !== FALSE) {
if ($line == 1) {
// This is the second line.
break;
}
$line++;
}
或者使用file()
打开文件,并使用下标[1]
访问行。
我会使用 SplFileObject 类...
$file = new SplFileObject("filename");
if (!$file->eof()) {
$file->seek($lineNumber);
$contents = $file->current(); // $contents would hold the data from line x
}
$handle = @fopen('test.txt', "r");
if ($handle) {
while (!feof($handle)) {
$lines[] = fgets($handle, 4096);
}
fclose($handle);
}
print_r($lines);
第二行使用$lines[1]
$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);
file()
或file_get_contents()
,在实践中是不建议的。对于小文件,这很好用。 - Phoenix这个问题现在已经很老了,但是对于处理非常大的文件的人来说,这里有一个解决方案,它不需要读取每一行之前的内容。这也是我在处理大约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个字符,然后从这些数据中获取第一行完整的内容。
$text = shell_exec("sed -n '74,159p' path/to/file.log");
如果您的文件比较大,这种解决方案就很不错。
while(!feof($file))
{
echo fgets($file). "<br />";
}
fclose($file);
我喜欢 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));