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

相关文章

Eclipse的PHP插件PHPEclipse安装和使用

Eclipse的PHP插件PHPEclipse安装和使用

PHPEclipse是Eclipse的一个插件,提供了包括PHP语法分析、运行、调试等功能的集成开发环境。它基于Eclipse的插件机制,即插即用,配置和使用都非常方便。如果平时需要同时...

php实现ip白名单黑名单功能

这个是一个检测ip是否非法的php函数,适应于白名单,黑名单功能开发,主要场景应用于:api来源限制,访问限制等. 复制代码 代码如下: /**  * 安全IP检测,支持IP段...

PHP的范围解析操作符(::)的含义分析说明

今天看到几个有关PHP的符号。一个是@,这个加在一个变量的前面,是为了抑制PHP解释器报错,也就是说即使出了错也不会显示出来。 还有一个更重要的符号PHP的范围解析操作符(::) 在没有...

php代码调试利器firephp安装与使用方法分析

php代码调试利器firephp安装与使用方法分析

本文实例分析了php代码调试利器firephp安装与使用方法。分享给大家供大家参考,具体如下: firephp简述 如果你曾经写过js代码的话,那么你对如下的代码肯定不会陌生: co...

PHP实现多文件上传的方法

本文实例讲述了PHP实现多文件上传的方法。分享给大家供大家参考。具体实现方法如下: <?php define('ROOT','D:/Program Files/www/...