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源码分析之DZX1.5字符串截断函数cutstr用法

本文实例讲述了php源码分析之DZX1.5字符串截断函数cutstr用法。分享给大家供大家参考。具体分析如下: <?php /** * 函数来源DZX1.5,文件所...

PHP常用特殊运算符号和函数总结(php新手入门必看)

注解符号:         // 单行注解      &...

PHP导出Excel实例讲解

本次实现PHP导出Excel文件使用的是PHP开源程序PHPExcel,部分关键代码分享给大家,具体内容如下 <?php error_reporting(E_ALL)...

php中array_unshift()修改数组key注意事项分析

本文实例分析了php中array_unshift()修改数组key注意事项。分享给大家供大家参考,具体如下: 众所周知,array_unshift()用来在数组的开头添加元素,但今天突然...

php简单实现文件或图片强制下载的方法

本文实例讲述了php简单实现文件或图片强制下载的方法。分享给大家供大家参考,具体如下: //下载 function downregcaseAction() { $file="up...