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程序的方法

调用方法:<script language = "javascript" src = "count.php"></script> count.php源码:复制代码...

关于查看MSSQL 数据库 用户每个表 占用的空间大小

关于查看MSSQL 数据库 用户每个表 占用的空间大小

最近做项目需要查看数据用户表的大小,包括记录条数和占用的磁盘空间数目。在网上找了很久其中查看MSSQL数据库每个表占用的空间大小 相对还可以。不过它的2、3中方法返回的数据比较多,有些是...

一些关于PHP的知识

1、如何配置PhpMyAdmin2.9 网络上很多教程的配置文件是针对PhpMyAdmin底版本的,一开始连2.9配置文件都不知道放哪里? 配置文件相对地址是:config.sample...

PHPer 需要了解的 5 个 Composer 小技巧

Composer是新一代的PHP依赖管理工具。其介绍和基本用法可以看这篇《PHP管理依赖(dependency)关系工具 Composer 安装与使用》。本文介绍使用Composer的五...

php一些错误处理的方法与技巧总结

1:为什么我得不到变量 我在一网页向另一网页POST数据name,为什么输出$name时却得不到任何值? 在PHP4.2以后的版本中register_global默认为off 若想取得...