Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the wrapper class of PHP reading large CSV files by line quickly

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the relevant knowledge of "how to achieve PHP to quickly read the wrapper class of large CSV files by line". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Reading large CSV files has been described earlier (PHP reads line by line and handles code examples of larger CSV files), but there are still some problems with how to manipulate large files quickly and completely.

1. How to quickly get the total number of lines of large CSV files?

Method 1: get the contents of the file directly and split it with newline characters to get the total number of lines. This method is feasible for small files but not feasible for large files.

Method 2: use fgets line by line traversal to get the total number of lines, this method is better than the first method, but large files are still possible to time out

Method 3: with the help of the SplFileObject class, directly locate the pointer to the end of the file, through the SplFileObject::key method to get the total number of lines, this method is feasible and efficient.

Specific implementation method:

The copy 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 files?

The SplFileObject class of PHP is still used, and fast positioning is achieved through the seek method.

The copy code is as follows:

$csv_file = 'path/bigfile.csv'

$start = 1000000; / / start reading from line 100000

$num = 100; / / read 100 lines

$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. Synthesize the above two points and organize them into a class for reading csv files:

The copy 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 copy 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, but also for other text types of large files or super-large files, as long as the fgetcsv method in the class can be slightly changed to current.

This is the end of the content of "how to achieve PHP to quickly read the wrapper class of large CSV files by line". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report