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实现插入排序

<?php /** * 插入排序 * @param Array $a 无序集合 * @return Array 有序集合 */ function insertS...

php技巧小结【推荐】

1 如何定义linux和window通用的文件分隔符号 DIRECTORY_SEPARATOR : 目录分隔符,是定义php的内置常量。在调试机器上,在windows我们习惯性的使用“\...

详解php中空字符串和0之间的关系

前言 最近在处理关于经纬度的问题时,在建表的时候,选择用字符串varchar存储经度、纬度。为以后的问题埋下伏笔。下面话不多说,我们来看看详细的介绍。 $_x=$row["x"];...

php实现将HTML页面转换成word并且保存的方法

本文实例讲述了php实现将HTML页面转换成word并且保存的方法。分享给大家供大家参考,具体如下: 这里用使用到一个PHP的工具叫:PHPWord。 生成Word的原理是,将堆规定好了...

PHP性能分析工具XHProf安装使用教程

HProf是facebook开源出来的一个php轻量级的性能分析工具,跟Xdebug类似,但性能开销更低,还可以用在生产环境中,也可以由程序开关来控制是否进行profile。基于浏览 器...