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贪婪算法解决0-1背包问题实例分析

本文实例讲述了PHP贪婪算法解决0-1背包问题的方法。分享给大家供大家参考。具体分析如下: 贪心算法解决0-1背包问题,全局最优解通过局部最优解来获得!比动态规划解决背包问题更灵活!...

php获取目录中所有文件名及判断文件与目录的简单方法

一,php获取目录中的所有文件名 1、打开要操作目录的目录句柄 代码示例: //打开当前目录下的目录pic下的子目录common。 $handler = opendir('pic/c...

php文件包含目录配置open_basedir的使用与性能详解

1.open_basedir介绍 open_basedir 将php所能打开的文件限制在指定的目录树中,包括文件本身。当程序要使用例如fopen()或file_get_contents(...

PHP分享图片的生成方法

PHP分享图片的生成方法

最近工作需求需要生成分享图片,最初用js的html2canvas截图插件各种问题,后来干脆PHP的PG库在后台生成图片,很愉快的解决了各种问题,我们要实现的效果如下图: 假设代码中用到...

php创建桌面快捷方式实现方法

php创建桌面快捷方式实现方法

第一种情况:php生成网页桌面快捷方式 将介绍使用php生成网页桌面快捷方式的代码,并添加图标及解决不同浏览器保存出现的乱码问题。 我们访问网站时,如果网站的内容很有吸引,一般我们都会使...