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

相关文章

无刷新动态加载数据 滚动条加载适合评论等页面

无刷新动态加载数据 滚动条加载适合评论等页面

滚屏加载更多数据,适合评论等页面 本例的数据库很简单,一看就明了  复制代码 代码如下: <div id="container"> <?php $query=...

让你成为更出色的PHP开发者的10个技巧

Raphael Caixeta作为一个刚开始使用PHP的开发者,总结了十点PHP开发的技术,希望能对这门了不起的放言的初学者能有些帮助:1、尽量使用PHP自带的核心函数和类 2、巧用配置...

PHP缓存技术的使用说明

在大部份情况下我们的网站都会使用数据库作为站点数据存储的容器。当你执行一个SQL查询时,典型的处理过程是:连接数据库->准备 SQL查询->发送查询到数据库->取得数据...

php.ini中的request_order推荐设置

今天刚刚安装dede,安装完成由一条错误信息(PHP 5.3 and above) Please set 'request_order' ini value to include C,G...

PHP实现的注册,登录及查询用户资料功能API接口示例

本文实例讲述了PHP实现的注册,登录及查询用户资料功能API接口。分享给大家供大家参考,具体如下: 服务端 <?php require 'conn.php'; head...