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用static方法的原因

静态方法不需要所在类被实例化就可以直接使用。 静态方法效率上要比实例化高,静态方法的缺点是不自动进行销毁,而实例化的则可以做销毁。 静态方法和静态变量创建后始终使用同一块内存,而使用实例...

php从csv文件读取数据并输出到网页的方法

本文实例讲述了php从csv文件读取数据并输出到网页的方法。分享给大家供大家参考。具体实现方法如下: <?php $fp = fopen('sample.csv','r...

php实现修改新闻时删除图片的方法

本文实例讲述了php实现修改新闻时删除图片的方法。分享给大家供大家参考。具体实现方法如下: //old_contents:待修改的公告内容; //$content:修改后的公告内容...

PHP使用GETDATE获取当前日期时间作为一个关联数组的方法

本文实例讲述了PHP使用GETDATE获取当前日期时间作为一个关联数组的方法。分享给大家供大家参考。具体分析如下: PHP GETDATE函数是用来获得当前的日期和时间,从操作系统或一个...

PHP curl CURLOPT_RETURNTRANSFER参数的作用使用实例

获取页面内容,不直接输出到页面,CURLOPT_RETURNTRANSFER参数设置    使用PHP curl获取页面内容或提交数据,有时候希望返回的内容...