PHP Quick Line Read CSV large file Package class share _php tutorial

Source: Internet
Author: User
Tags spl
This article mainly introduces a PHP fast line read CSV large file package class, this class also applies to other large text files, the need for friends can refer to the following

The read of the CSV large file has been described earlier (PHP reads the code instance of the larger CSV file by line), but there are still some problems with how to quickly and fully manipulate large files.

1, how to quickly get the total number of CSV large file?

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 row by line traversal, arrive the total number of rows, this method is better than the method, but the large file still has the possibility of timeout;

Method Three: With the help of Splfileobject class, direct 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:

The code is 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 of large CSV file?

Still using PHP's Splfileobject class, the Seek method enables quick positioning.

The code is as follows:

$csv _file = ' path/bigfile.csv ';

$start = 100000; Start reading from line 100,000th

$num = 100; Read 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, combined with the above two points, organized into a CSV file read class:

The code is 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;

}

}

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;

}

Public Function Get_csv_file () {

return $this->csv_file;

}

Private Function _file_valid ($file = ") {

$file = $file? $file: $this->csv_file;

if (! $file | |!file_exists ($file)) {

return false;

}

if (!is_readable ($file)) {

return false;

}

return true;

}

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_file, ' RB ');

}

return true;

}

Public Function get_data ($length = 0, $start = 0) {

if (! $this->_open_file ()) {

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;

}

Public Function Get_lines () {

if (! $this->_open_file ()) {

return false;

}

$this->spl_object->seek (filesize ($this->csv_file));

return $this->spl_object->key ();

}

Public Function Get_error () {

return $this->error;

}

}

The calling method is as follows:

The code is as follows:

Include (' CsvReader.class.php ');

$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 oversized files are also available, provided that the class Fgetcsv method is slightly changed to current.

http://www.bkjia.com/PHPjc/752682.html www.bkjia.com true http://www.bkjia.com/PHPjc/752682.html techarticle This article mainly introduces a PHP fast line read CSV large file package class, this class also applies to other large text files, the need for friends can refer to the next CSV big text ...

  • Related Article

    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.