2011-02-15 65 views

回答

6

這是這樣做的一種方式。也許這有點矯枉過正,因爲它效率不高。 (使用file()會更快)

$content = file_get_contents($filename); 
$lines = explode("\n", $content); 
$skipped_content = implode("\n", array_slice($lines, 2)); 
1

是的,但使用file_get_contents它會太複雜。我建議使用file()函數:

$file_array = file("yourfile.txt"); 
unset($file_array[0]); 
unset($file_array[1]); 
file_put_contents("outfile.txt", implode("", $file_array)); 
+3

file()會將整個文件加載到內存中,只是想提到這一點。我會用fopen()去手動讀取文件,尋找換行符,或者使用strpos($ content,「\ n」)並剪切這部分字符串。 :) – ludesign 2011-02-15 20:07:07

+0

@ludesign:有效的點,謝謝。你可以發表你的答案和示例代碼:) – 2011-02-15 20:08:22

0

使用文件(),然後取消設置第一2個數組鍵然後破滅

0

如果線路不是很長你就不能使用正則表達式上讀取文件?從php手冊有file_get_contents偏移量參數,雖然這很可能不會有用,因爲那樣你需要事先知道行長度。也許file_get_contents不適合在這種情況下使用的函數?