Csv读写类

时间:2021-12-08 01:21:23
 <?php

 /**
* CSV 文件读写类
*
* 示例: $header = array('name' => '名字', 'age' =>'年龄', 'idcard' => '身份证号'); $data = array(
array('age' => 12, 'name' => '李四', 'idcard' => '42088751564616131312'),
(object) array('idcard' => '72888751564616131312', 'age' => 17, 'name' => '张三'),
); Csv::instance()->open('e:/abc1.csv', true)->header($heaer)->create($data)->close();
Csv::instance()->open()->header($header)->create($data)->download('用户数据.csv'); */ class Csv { public $header = array(); public $fp = ''; public static function instance(){ return new static();
} /**
* 打开/创建文件
* @param string $file
* @param string $mode
* @return $this
*/
public function open($file = '', $mode = 'a'){ if(empty($file)){ $this->fp = tmpfile();
}else{
if($mode === true) $mode = 'w+';
$this->fp = fopen(iconv('UTF-8', 'GBK', $file), $mode);
} return $this; } /**
* 写入表头
* @param $value
* @return $this
*/
public function header($value){ $this->header = is_array($value)? $value : (array) $value;
fputcsv($this->fp, array_values($this->header)); return $this;
} /**
* 写入一行
* @param $data
* @return $this
*/
public function write($data){ if(is_string($data)){
$data = json_decode($data, true);
}else if(!is_array($data)){
$data = json_decode(json_encode($data), true);
} if(empty($data)) return $this; if($this->header){
$column = array();
foreach($this->header as $key => $value){
$column[$key] = isset($data[$key])? $data[$key] : 'NULL';
}
$data = &$column;
} foreach($data as $key => &$value){
if(is_numeric($value) && strlen($value) > 10){
$value .= "\t";
}else if(!is_scalar($value)){
$value = json_encode($value, JSON_UNESCAPED_UNICODE);
} } fputcsv($this->fp, $data); return $this;
} /**
* 读取一行
* @param int $length
* @param string $delimiter
* @param string $enclosure
* @param string $escape
* @return array|false|null
*/
public function read($length = 0, $delimiter = ',', $enclosure = '"', $escape = '\\'){ return fgetcsv($this->fp, $length, $delimiter, $enclosure, $escape);
} /**
* 获取文件内容
* @param null $callback
* @param int $start
* @param int $length
* @return array
*/
public function content($callback = null, $start = 1, $length = 0){ if($start > 1){
for($start; $start > 1; $start--){
$this->read(1);
}
} $data = array();
$key = 0;
while(($row = $this->read()) !== false){ if($length < 0) break;
if($length > 0) $length--; if(is_callable($callback)){
$row = $callback($row);
if($row === null) continue;
}
$data[$key] = $row;
$key++; } return $data;
} /**
* 下载
* @param $file
*/
public function download($file){ $file = iconv('UTF-8', 'GBK', $file);
header('Content-Type:application/application/octet-stream');
header("Content-Disposition:attachment;filename=$file"); fseek($this->fp, 0);
echo stream_get_contents($this->fp); $this->close(); } /**
* 快速写入文件
* @param $data
* @return $this
*/
public function create($data){ if(is_string($data)) $data = json_decode($data, true);
foreach($data as $row){
$this->write($row);
} return $this; } public function close(){ fclose($this->fp);
} }