PHP Fast read csv large file by line of package class sharing

Source: Internet
Author: User
Tags spl

  This article mainly introduces a PHP fast line reading CSV large file Package class, this class also applies to other large text files, need friends can refer to the following

The reading of the CSV large file has been described earlier (PHP reads by row, processing the code instance of the larger CSV file), but there are still some problems in how to quickly complete the operation of large files.   1, how to quickly get the total number of CSV large files?   Method One: Directly obtain the contents of the file, using newline characters to split the total number of rows, this method is feasible for small files, processing large files is not feasible; method Two: Use fgets line traversal, the total number of rows, this method is better than the method, but the large file still has the possibility of timing out; Method Three: With the help of Splfileobject class, directly position the pointer to the end of the file, through the Splfileobject::key method to obtain the total number of rows, this approach is feasible and efficient.   Specific implementation methods:     Code as follows: $csv _file = ' path/bigfile.csv '; $SPL _object = new Splfileobject ($csv _file, ' RB '); $SPL _object->seek (filesize ($csv _file)); echo $SPL _object->key ();   2, how to quickly get the data csv large files?   still uses PHP's Splfileobject class to achieve fast positioning through the Seek method.   Code as follows: $csv _file = ' path/bigfile.csv '; $start = 100000;  //reads $num = 100 from line 100,000th;    //reads 100 rows $data = Array (); $SPL _object = new Splfileobject ($csv _file, ' RB '); $SPL _object->seek ($start); while ($num-&& $spl _object->eof ()) {  $data [] = $spl _object->fgetcsv ();   $SPL _object-> Next (); } print_r ($data);     3, synthesis above two points, sorted into a CSV file read class:   code as follows: Class Csvreader { private $csv _file;  private $SPL _object = null;  private $error;    public function __construct ($csv _file = ') {  if ($csv _file && file_exists ($csv _file)) {    $this->csv_file = $csv _file;  } &nbsp    public function set_csv_file ($csv _file) {  if (! $csv _file | |!file_exists ($csv _file)) {     $this->error = ' File invalid ';    return false;  }   $this->csv_file = $csv _file;   $this->spl_object = null; &NBSP}    public function Get_csv_file () {  return $this->csv_file;  }    private funct Ion _file_valid ($file = ') {  $file = $file? $file: $this->csv_file;   if (! $file | |!file_exists ($file)) {   return false;  }   if (!is_readable ($file)) {   return false;  }   return T Rue &NBSP}    private function _open_file () {  if (! $this->_file_valid ()) {    $this->error = ' File invalid ';    return false;  }   if ($this->spl_object = null) {    $this->spl_object = new Splfileobject ($this->csv_fi Le, ' RB ');  }   return true; &NBSP}    public function get_data ($length = 0, $start = 0) {  if (! $this->_open_file ()) {  &NBSP ; return false;  }   $length = $length? $length: $this->get_lines ();   $start = $start-1;   $start = ($start < 0)? 0: $start;   $data = array ();   $this->spl_object->seek ($start);   while ($length-&& $this->spl_object->eof ()) {    $data [] = $this->spl_object-> Fgetcsv ();     $this->spl_object->next ();  }   return $data; &NBSP}    public function Get_lines () {  if (! $this->_open_file ()) {   return false;   }   $this->spl_object->seek (filesize ($this->csv_file));   return $this->spl_object->key (); &NBSP}    pubLic function Get_error () {  return $this->error; &nbsp}}   The calling method is as follows:   The code is as follows: include (' CsvReader.class.ph P ');   $csv _file = ' path/bigfile.csv ';   $csvreader = new Csvreader ($csv _file);   $line _number = $csvreader->get_lines ();   $data = $csvreader->get_data (10);       echo $line _number, Chr (10);   Print_r ($data);       In fact, the above Csvreader class is not only for CSV large files, for other text types of large files or large files are also available, provided that the class in the Fgetcsv method slightly changed to current can.  

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.