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

相关文章

php5中date()得出的时间为什么不是当前时间的解决方法

相关设置是修改php.ini中的 date.timezone 参数: [Date] ; Defines the default ...

php命令行(cli)下执行PHP脚本文件的相对路径的问题解决方法

在php命令行下执行.php文件时,执行环境的工作目录(getcwd( ))是php命令程序(php.exe)所在目录,所以如果想在文件内使用相对路径时,要先切换当前的工作目录才行。 小...

PHP+JS+rsa数据加密传输实现代码

JS端代码: 复制代码 代码如下: //文件base64.js: var b64map="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv...

ThinkPHP路由详解

有了基本配置,我们就可以来访问我们的应用默认首页了。进入到项目目录,可以直接使用PHP内置服务器来开始访问,比如: php -S localhost:8999 浏览器输入loc...

PHP的拦截器实例分析

本文实例讲述了PHP的拦截器用法。分享给大家供大家参考。具体如下: PHP提供了几个拦截器,用于在访问未定义的方法和属性时被调用,如下所示: 1、__get($property) 功能:...