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程序设计有所帮助。

相关文章

PHP实现给定一列字符,生成指定长度的所有可能组合示例

PHP实现给定一列字符,生成指定长度的所有可能组合示例

本文实例讲述了PHP实现给定一列字符,生成指定长度的所有可能组合。分享给大家供大家参考,具体如下: 给定一列字符,生成指定长度的所有可能的组合: 如:a,b,c,d,e 或 0-9&nb...

利用PHP实现智能文件类型检测的实现代码

使用文件后缀和MIME类型检测 通常我们想严格限制文件类型的时候,可以简单地用$_FILES['myFile']['type']  取得文件的 MIME类型然后来检测它是否是合法的类型。...

解析PHP中ob_start()函数的用法

ob_start()函数用于打开缓冲区,比如header()函数之前如果就有输出,包括回车/空格/换行/都会有"Header had all ready send by"的错误,这时可以...

PHP显示今天、今月、上月、今年的起点/终点时间戳的代码

$t = time(); $t1 = mktime(0,0,0,date(“m”,$t),date(“d”,$t),date(“Y”,$t)); $t2 = mktime(0,0,0,d...

php错误级别的设置方法

PHP在运行时, 针对严重程度不同的错误,会给以不同的提示。 eg:在$a没声明时,直接相加,值为NULL,相加时当成0来算.但是,却提示NOTICE,即注意. 我们在开发中, 为了程序...