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安装BCMath扩展的方法

PHP安装BCMath扩展的方法

编译安装 (PHP-5.6.23) 1.进入PHP源码包目录下的ext/bcmath目录。 [root@192 bcmath]# ls bcmath.c config.m4...

PHP输出XML格式数据的方法总结

PHP输出XML格式数据的方法总结

本文实例讲述了PHP输出XML格式数据的方法。分享给大家供大家参考,具体如下: 方法1: <?php header("Content-type: text/xml");...

php报表之jpgraph柱状图实例代码

php报表之jpgraph柱状图实例代码

新手初识jpgraph肯定会遇到各种各样的问题,比如乱码什么的,本案例是jpgraph3.0.7制作,也经过本人的多次实验,解决乱码问题 复制代码 代码如下: <?php $dat...

PHP实现HTML生成PDF文件的方法

本文实例讲述了在linux中利用HTML2FPDF与wkhtmltoimage把网页html直接生成pdf格式的文件方法,分享给大家供大家参考。具体实现方法如下: 找到一款在FPDF和H...

PHP设计模式之解释器模式的深入解析

PHP设计模式之解释器模式的深入解析

解释器(Interpreter)模式,它包括一个具有复合类分层结构的文法表现,规则是映射到类,跟随在文法后面的表达式可以被转换成一个抽象的语法树,除了复合模式的实例对象图外,没有别的内容...