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+javascript的日历控件

复制代码 代码如下:<html> <head> <title>js calendar</title> <script languag...

PHP call_user_func和call_user_func_array函数的简单理解与应用分析

本文实例讲述了PHP call_user_func和call_user_func_array函数的简单理解与应用。分享给大家供大家参考,具体如下: call_user_func():调用...

PHP+redis实现微博的推模型案例分析

本文实例讲述了PHP+redis实现微博的推模型。分享给大家供大家参考,具体如下:最近在看了一下关于redis的内容,然后利用redis写了一个简单的微博项目,这篇文章是关于推模型的。推模型所谓推模型...

php 高性能书写

从.NET转去做PHP4年了,最近开始追求高性能了~~ 所以开始觉得是时候要写写博客了~ 来段发现物先~ 复制代码 代码如下: $arr = array( 'attr1' => 1...

php循环语句 for()与foreach()用法区别介绍

for 循环是 PHP 中最复杂的循环结构。它的行为和 C 语言的相似。 for 循环的语法是: for (expr1; expr2; expr3) statement 第一个表达式(e...