PHP快速按行讀取CSV大文件的封裝類分享(也適用于其它超大文本文件)
CSV大文件的讀取已經(jīng)在前面講述過了(PHP按行讀取、處理較大CSV文件的代碼實(shí)例),但是如何快速完整的操作大文件仍然還存在一些問題。
1、如何快速獲取CSV大文件的總行數(shù)?
辦法一:直接獲取文件內(nèi)容,使用換行符進(jìn)行拆分得出總行數(shù),這種辦法對(duì)小文件可行,處理大文件時(shí)不可行;
辦法二:使用fgets一行一行遍歷,得出總行數(shù),這種辦法比辦法一好一些,但大文件仍有超時(shí)的可能;
辦法三:借助SplFileObject類,直接將指針定位到文件末尾,通過SplFileObject::key方法獲取總行數(shù),這種辦法可行,且高效。
具體實(shí)現(xiàn)方法:
$csv_file = 'path/bigfile.csv';
$spl_object = new SplFileObject($csv_file, 'rb');
$spl_object->seek(filesize($csv_file));
echo $spl_object->key();
2、如何快速獲取CSV大文件的數(shù)據(jù)?
仍然使用PHP的SplFileObject類,通過seek方法實(shí)現(xiàn)快速定位。
$csv_file = 'path/bigfile.csv';
$start = 100000; // 從第100000行開始讀取
$num = 100; // 讀取100行
$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、綜合上面兩點(diǎn),整理成一個(gè)csv文件讀取的類:
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;
}
}
調(diào)用方法如下:
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);
其實(shí),上述CsvReader類并不只針對(duì)CSV大文件,對(duì)于其他文本類型的大文件或超大文件同樣可用,前提是將類中fgetcsv方法稍加改動(dòng)為current即可。
相關(guān)文章
創(chuàng)建數(shù)據(jù)庫php代碼 用PHP寫出自己的BLOG系統(tǒng)
今天的任務(wù)是創(chuàng)建數(shù)據(jù)庫,因?yàn)閷?duì)數(shù)據(jù)庫懂的很少,所以在數(shù)據(jù)庫表關(guān)系上還很差啊。2010-04-04使用Git實(shí)現(xiàn)Laravel項(xiàng)目的自動(dòng)化部署
這篇文章主要介紹了使用Git實(shí)現(xiàn)Laravel項(xiàng)目的自動(dòng)化部署,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11thinkPHP5.0框架獨(dú)立配置與動(dòng)態(tài)配置方法
這篇文章主要介紹了thinkPHP5.0框架獨(dú)立配置與動(dòng)態(tài)配置方法,結(jié)合實(shí)例形式分析了thinkPHP5.0框架獨(dú)立配置與靜態(tài)配置的功能、實(shí)現(xiàn)技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-03-03php導(dǎo)出word文檔與excel電子表格的簡單示例代碼
本篇文章主要是對(duì)php導(dǎo)出word文檔與excel電子表格的簡單示例代碼進(jìn)行了介紹,需要的朋友可以過來參考下,希望對(duì)大家有所幫助2014-03-03php如何修改SESSION的生存存儲(chǔ)時(shí)間的實(shí)例代碼
本篇文章主要介紹了php如何修改SESSION的生存時(shí)間的實(shí)例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-07-07PHP 將數(shù)組打亂 shuffle函數(shù)的用法及簡單實(shí)例
下面小編就為大家?guī)硪黄狿HP 將數(shù)組打亂 shuffle函數(shù)的用法及簡單實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-06-06