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注释实例技巧

复制代码 代码如下:<?php $a = 1; $b = 2; if (1==1) { $andy = '帅哥'; } ?> 一般注释的时候,用 复制代码 代码如下:<...

php实现httpRequest的方法

本文实例讲述了php实现httpRequest的方法。分享给大家供大家参考。具体如下: 想从学校图书馆的网站上抓取数据处理之后在返回给浏览器,试了不少方法。首先试了http_reques...

php中json_decode()和json_encode()的使用方法

1.json_decode() json_decode (PHP 5 >= 5.2.0, PECL json >= 1.2.0) json_decode — 对 J...

php修改时间格式的代码

修改时间格式: date("Y-m-d",strtotime($list['pubdate'])); 学习解释:将时间放入strtotime为时间戳后用date()转化格式.下面写了两个...

PHP5.6读写excel表格文件操作示例

本文实例讲述了PHP5.6读写excel表格文件操作。分享给大家供大家参考,具体如下: 测试环境:php5.6.24.这块没啥兼容问题。 需要更多栗子,请看PHPExcel的exampl...