php遍历CSV类实例

yipeiwu_com6年前PHP代码库

本文实例讲述了php遍历CSV类。分享给大家供大家参考。具体如下:

<?php
class CSVIterator implements Iterator
{ 
  const ROW_SIZE = 4096;
  private $filePointer;
  private $currentElement;
  private $rowCounter;
  private $delimiter;
  public function __construct( $file, $delimiter = ',' )
  {
    $this->filePointer = fopen( $file, 'r' );
    $this->delimiter  = $delimiter;
  }
  public function rewind()
  {
    $this->rowCounter = 0;
    rewind( $this->filePointer );
  }
  public function current()
  {
    $this->currentElement = fgetcsv($this->filePointer,self::ROW_SIZE,$this->delimiter);
    $this->rowCounter++;
    return $this->currentElement;
  }
  public function key()
  {
    return $this->rowCounter;
  }
  public function next()
  {
    return !feof( $this->filePointer );
  }
  public function valid()
  {
    if( !$this->next() )
    {
      fclose( $this->filePointer );
      return FALSE;
    }
    return TRUE;
  }
} // end class
?>

希望本文所述对大家的php程序设计有所帮助。

相关文章

通达OA公共代码 php常用检测函数

check_type.php(使用类型检验函数) 复制代码 代码如下: <?php /*********************/ /* */ /* Version : 5.1.0...

smarty section简介与用法分析

基本原形为: {section name = name loop = $varName[, start = $start, step = $step, max = $max, show...

PHP数组编码gbk与utf8互相转换的两种方法

一、利用var_export(), eval()方法 /** * 将含有GBK的中文数组转为utf-8 * * @param array $arr 数组 * @para...

php json_encode奇怪问题说明

json_encode 只支持utf-8格式这个就不多说了 复制代码 代码如下: $array = array ( [0] => array ( [sale_unit_detail...

php实现压缩合并js的方法【附demo源码下载】

本文实例讲述了php实现压缩合并js的方法。分享给大家供大家参考,具体如下: test.php文件如下: require_once('jsmin.php'); $files = gl...