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程序设计有所帮助。

相关文章

LINUX下PHP程序实现WORD文件转化为PDF文件的方法

本文实例讲述了LINUX下PHP程序实现WORD文件转化为PDF文件的方法。分享给大家供大家参考,具体如下: <?php set_time_limit(0); func...

php常用的工具开发整理

php常用的工具开发整理

PHP开发工具及其优缺点 首先,可以用记事本来开发。 记事本每个人的电脑上都有,也就是我们常说的txt文件。把txt这个后缀更改为点PHP就可以了。然后该怎么编辑就怎么编辑。缺点是, 没...

PHP实现的栈数据结构示例【入栈、出栈、遍历栈】

本文实例讲述了PHP实现的栈数据结构。分享给大家供大家参考,具体如下: 利用php面向对象思想,栈的属性有top、最大存储数、和存储容器(这里利用了php数组)。 代码如下:实现了入栈、...

PHP 万年历实现代码

PHP 万年历实现代码

使用PHP实现万年历功能的要点: •得到当前要处理的月份总共有多少天$days •得到当前要处理的月份的一号是星期几$dayofweek $days的作用:知道要...

PHP正则替换函数preg_replace和preg_replace_callback使用总结

在编写PHP模板引擎工具类时,以前常用的一个正则替换函数为 preg_replace(),加上正则修饰符 /e,就能够执行强大的回调函数,实现模板引擎编译(其实就是字符串替换)。 详情介...