我想從使用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