PHP code for reading the content from rows X to lines Y of large files _ PHP Tutorial

Source: Internet
Author: User
PHP implements code for reading the content from row X to line Y of a large file. I need to read several lines of content from a file, but the file is large. so I studied how php can read several lines of content from a large file and wrote a method, the code is as follows (with comments added): Slow reading of several lines of content in a file, but the file is large. so I studied how php can read several lines of content in a large file, I wrote a method and the code is as follows (with comments added ):
If the cached file can be saved in one row, and the algorithm is used to read the specified number of rows, it is naturally much faster to select than to read all. however, php seems to be relatively weak in this aspect and is not very easy to operate. even if SplFileObject is used, memory pressure exists.

The code is as follows:


$ Fp-> seek ($ startLine-1 );



After testing, this line of code reaches the last line in the 8 MB text, and the memory usage is 49KB, which is not bad. if you use the fgets skip mode in the fopen mode, it will consume 29KB of memory. the fopen mode is dominant.

The code is as follows:


Function getFileLines ($ filename, $ startLine = 1, $ endLine = 50, $ method = 'RB '){
$ Content = array ();

If (version_compare (PHP_VERSION, '5. 1.0 ','> = ') {// Determine the php version (because SplFileObject, PHP> = 5.1.0)
$ Count = $ endLine-$ startLine;
$ Fp = new SplFileObject ($ filename, $ method );
$ Fp-> seek ($ startLine-1); // go to row N, and the seek method parameter starts counting from 0.
For ($ I = 0; $ I <= $ count; ++ $ I ){
$ Content [] = $ fp-> current (); // current () get the content of the current row
$ Fp-> next (); // next row
}
} Else {// PHP <5.1
$ Fp = fopen ($ filename, $ method );
If (! $ Fp)
Return 'Error: can not read file ';
For ($ I = 1; $ I <$ startLine; ++ $ I) {// skip the previous $ startLine line
Fgets ($ fp );
}

For ($ I; $ I <= $ endLine; ++ $ I ){
$ Content [] = fgets ($ fp); // read the content of the file row
}
Fclose ($ fp );
}
Return array_filter ($ content); // array_filter: false, null ,''
}



The results are good, and the SplFileObject class features better.

Slow (with comments added): slow...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.