我有这个文件结构:
line1 (number or a a short string)
line2 (can be several MB of text)
;
line1
line2
;
line1
line2
;
...
Run Code Online (Sandbox Code Playgroud)
总文件大小超过100MB,因此每次逐行读取都相当慢.我想只读取每个块的"line1"并跳过所有"line2".或者只是读一行我知道的亚麻布.有什么办法可以用php做到吗?读取线条的标准方法将线条记入存储器中,并且对于这种结构而言并不那么有效.
(我知道数据库结构会更好用,但这是一个研究案例,我真的想要一个解决方案.)
Nim*_*007 14
使用splfileobject
无需逐行读取所有行
可以"跳"到所需的线
如果您知道行号:
//lets say you need line 4
$myLine = 4 ;
$file = new SplFileObject('bigFile.txt');
//this is zero based so need to subtract 1
$file->seek($myLine-1);
//now print the line
echo $file->current();
Run Code Online (Sandbox Code Playgroud)
退房:http: //www.php.net/manual/en/splfileobject.seek.php