2011-02-15 33 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不适合在这种情况下使用的函数?