2011-04-25 116 views
37

我想从使用php的文本文件读取特定行。 这里的文本文件:PHP:从文件中读取特定行

foo 
foo2 

我该如何获得使用PHP的第二行的内容? 这会返回第一行:

<?php 
$myFile = "4-24-11.txt"; 
$fh = fopen($myFile, 'r'); 
$theData = fgets($fh); 
fclose($fh); 
echo $theData; 
?> 

..但我需要第二行。

任何帮助,将不胜感激

回答

69
$myFile = "4-24-11.txt"; 
$lines = file($myFile);//file in to an array 
echo $lines[1]; //line 2 

file — Reads entire file into an array

+2

注意:应该是'$ lines [1]' – 2011-04-25 05:33:08

+1

谢谢你!这正是我需要的!另外,感谢您的回答如此之快。 – 2011-04-25 05:53:51

+52

如果文件大小很大,这个解决方案会很慢并占用大量内存。 – Raptor 2012-08-30 09:02:54

2

你必须循环文件,直到文件末尾。

while(!feof($file)) 
    { 
    echo fgets($file). "<br />"; 
    } 
    fclose($file); 
+0

不是我需要的东西,但是谢谢你的回答 – 2011-04-25 05:54:41

15

如果你想这样做的......

$line = 0; 

while (($buffer = fgets($fh)) !== FALSE) { 
    if ($line == 1) { 
     // This is the second line. 
     break; 
    } 
    $line++; 
} 

或者,file()打开它,并与[1]下标线。

+0

所以基本上,把它喂入一个数组并且取出第二个项目。我懂了。谢谢。 – 2011-04-25 05:54:24

+0

@Sang第一个解决方案只是为了适应您的代码,因为它是现在。 – alex 2011-04-25 05:55:05

5
$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); 
+0

啊。我懂了。谢谢回答。 :) – 2011-04-25 05:55:02

+3

顺便说一句,如果您可能正在使用任何大文件,建议不要在文件中将整个文件提供给数组,例如'file()'或'file_get_contents()'。对于小文件,它的效果很好。 – Phoenix 2011-04-25 06:32:40

7

您可以使用以下方法来获取所有行的文件

$handle = @fopen('test.txt', "r"); 

if ($handle) { 
    while (!feof($handle)) { 
     $lines[] = fgets($handle, 4096); 
    } 
    fclose($handle); 
} 


print_r($lines); 

$lines[1]在你的第二个行

+0

谢谢兄弟!我很欣赏你回答的事实。 – 2011-04-25 05:59:40

14

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桌面上,所以它不具有代表性的生产环境,但仍......非常不同。

+0

这么晚的问题很好的答案,并有帮助。 +1 – Josiah 2016-04-10 00:59:32

+0

请记住,'SplFileObject'锁定了文件。因此,当不再需要类为null时(例如'$ spl = null;'),否则您将被拒绝对该文件执行一些其他操作 - 删除,重命名,在类之外访问等。 – Wh1T3h4Ck5 2017-10-14 16:57:29

3

如果您在Linux上使用PHP,你可以试试下面的阅读,例如第74和第159行之间的文字:

$text = shell_exec("sed -n '74,159p' path/to/file.log"); 

如果文件很大,此解决方案很好。

0

您可以尝试循环,直到您想要的行,而不是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; 
?> 
0

我喜欢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)); 
6

我会用SplFileObject类...

$file = new SplFileObject("filename"); 
if (!$file->eof()) { 
    $file->seek($lineNumber); 
    $contents = $file->current(); // $contents would hold the data from line x 
} 
+0

#Salute# Revoutionary真棒方法:) – 2017-01-19 04:26:11

0

这个问题是现在很老了,但任何人都处理非常大的文件,在这里是不涉及读取每一个解决方案前一行。这也是唯一的解决方案,在我的情况下工作约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个字符,然后抓住从数据的第一个完整产品线。