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 字符截取 解决中文的截取问题,不用mb系列

复制代码 代码如下:function Cut_string($string, $start ,$sublen, $extstring='...', $code = 'UTF-8') {/...

php漏洞之跨网站请求伪造与防止伪造方法

伪造跨站请求介绍伪造跨站请求比较难以防范,而且危害巨大,攻击者可以通过这种方式恶作剧,发spam信息,删除数据等等。这种攻击常见的表现形式有:   伪造链接,引诱用户点击,或是让用户在不...

dedecms系统的广告设置代码 基础版本

1.文章内每篇文章的结尾处,顶一下 踩一下的上方的广告位置, 这个位置的大小是658*60 658长度不要变,60高度可以任意改 广告代码添加在这—-后台—–模板—-默认模板管理—-找到...

PHP简洁函数(PHP简单明了函数语法)

1、与mysql相关 mysql_connect 建立一个与MySQL服务器的连接 语法 resource mysql_connect(string server[,string usi...

PHP生成随机字符串(3种方法)

如用户注册生成随机密码,用户重置密码也需要生成一个随机的密码。随机密码也就是一串固定长度的字符串,文章整理了几种生成随机字符串的方法。 方法一 1、在33 – 126中生成一个随机整数,...