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模糊查询技术实例分析【附源码下载】

本文实例讲述了PHP模糊查询技术。分享给大家供大家参考,具体如下: 简介 从本质上揭密PHP模糊查询技术 功能 根据输入的关键字查找相关用户 PHP用户查询器案例分析...

php split汉字

第一种办法: 加载 Encode模块,前提是你需要安装这个模块 例子代码: 复制代码 代码如下: $str=decode("gb2312",$names[$index]); @chars...

Windows下的PHP安装pear教程

因为需要安装 PHPUnit,要先装 pear,网上的教程大多数是以双击 go-pear.bat 开始,但是我安装的php文件夹里压根没有这个文件。 经过几次搜索之后终于找到了办法,解决...

在WordPress中获取数据库字段内容和添加主题设置菜单

在WordPress中获取数据库字段内容和添加主题设置菜单

get_option() 函数使用技巧 get_option()这个函数,实际上我们在整合后台功能的时候经常会用到的一个函数,主要用来从 WordPress 博客数据库 option 表...

php对数组内元素进行随机调换的方法

本文实例讲述了php对数组内元素进行随机调换的方法。分享给大家供大家参考。具体分析如下: 这是一个自定义的php数组元素随机调换的函数,php已经有一个内置的同样功能的函数shuffle...