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实现获取某个月份周次信息的方法

本文实例讲述了PHP实现获取某个月份周次信息的方法。分享给大家供大家参考。具体如下: <?php function getMonthweeks($date){ $r...

深入理解PHP原理之错误抑制与内嵌HTML分析

PHP提供了一个错误抑制符'@', 它是通过什么方式来阻止错误输出呢? 我又该在什么时候使用它呢? 这是这俩天一些网友提到的共同问题, 今天就索性整体回答下, 备后来人翻阅. PHP文件...

关于php unset对json_encode的影响详解

关于php unset对json_encode的影响详解

前言 PHP 中有个释放变量的语句叫做unset(从PHP4开始unset已经不再是一个函数了,而是一个语句),本文主要给大家介绍了关于php unset对json_encode影响的相...

PHP 文件扩展名 获取函数

复制代码 代码如下:<?php $file = "/home/lvyaozu/backup_20080115.txt"; for($i=1; $i < 6; $i++) {...

php通过淘宝API查询IP地址归属等信息

淘宝公司提供了一个很好用的IP地理信息查询接口。 在这里:http://ip.taobao.com/ TaobaoIPQuery2这个类将极大的简化相关的信息查询。 类 Taobao...