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 curl参数的详解

curl_setopt (PHP 4 >= 4.0.2) curl_setopt -- 为CURL调用设置一个选项 描述 bool curl_setopt (int ch, str...

PHP怎样用正则抓取页面中的网址

前言 链接也就是超级链接,是从一个元素(文字、图片、视频等)链接到另一个元素(文字、图片、视频等)。网页中的链接一般有三种,一种是绝对URL超链接,也就是一个页面的完整路径;另一种是相对...

WIN8.1下搭建PHP5.6环境

WIN8.1下搭建PHP5.6环境

  第一次接触php是在2014-5月份左右,当时是自己的主攻方向是C#,对php比较排斥, 其中很多一部分原因,就是PHP的断点调试一直无法配置成功,用echo打印日志的方式排错,使得...

功能强大的PHP POST提交数据类

本文实例为大家分享了PHP功能强大的 POST提交数据类,供大家参考,具体内容如下 <?php class Request{ public static fun...

PHP递归统计系统中代码行数

本文实例为大家分享了PHP递归统计系统中代码行数的具体代码,供大家参考,具体内容如下 1、统计代码行数,必然用到的两个关键的知识点:函数递归以及文件读取。 函数递归无非就是在函数的代码中...