php遍历CSV类实例

yipeiwu_com5年前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中curl、fsocket、file_get_content三个函数的使用比较

抓取远程内容,之前一直都在用file_get_content函数,其实早就知道有curl这么一个好东西的存在,但是看了一眼后感觉使用颇有些复杂,没有file_get_content那么简...

Swoole 1.10.0新版本发布,增加了多项新特性

前言 Swoole 可以广泛应用于互联网、移动通信、企业软件、云计算、网络游戏、物联网(IOT)、车联网、智能家居等领域。使用 PHP + Swoole 作为网络通信框架,可以使企业 I...

php保存二进制原始数据为图片的程序代码

得到post过来的二进制原始数据,选择一个生成路径及图片的名字,之后写入,思路很显而易见 //生成图片 $imgDir = 'uploadImg/'; $filena...

PHP 正则表达式之正则处理函数小结(preg_match,preg_match_all,preg_replace,preg_split)

前面我们已经学习了正则表达式的基础语法,包括了定界符、原子、元字符和模式修正 符。实际上正则表达式想要起作用的话,就必须借用正则表达式处理函数。本节我们就来介绍一下PHP中基于perl的...

PHP中CURL方法curl_setopt()函数的参数分享

PHP CURL curl_setopt 参数bool curl_setopt (int ch, string option, mixed value)curl_setopt()函数将为...