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 + nginx项目中的权限详解

本文给大家介绍的关于php + nginx项目权限的相关内容,分享出来供大家参考学习,下面来看看详细的介绍: nginx/php-fpm 进程权限 主进程用户为启动的用户...

php中spl_autoload详解

SPL有两个不同的函数 spl_autoload, spl_autoload_call,通过将autoload_func指向这两个不同的函数地址来实现不同的自动加载机制。 spl_aut...

php获取YouTube视频信息的方法

php获取YouTube视频信息的方法

本文实例讲述了php获取YouTube视频信息的方法。分享给大家供大家参考。具体分析如下: YouTube的视频地址格式 https://www.youtube.com/watch...

php 抽象类的简单应用

All right, 父类postParent定义为抽象,规定子类必须重新实现 buildHTML()方法,这个方法并没有花括号,如果有不管有没有内容都会报错的。 现在越看越觉得这代码完...

PHP file_get_contents设置超时处理方法

file_get_contents的超时处理 话说,从PHP5开始,file_get_content已经支持context了(手册上写着:5.0.0 Added the context...