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     $a=1; //开始     $b=2; ...

探讨PHP删除文件夹的三种方法

1、递归法 复制代码 代码如下:deleteDir($dir){if (rmdir($dir)==false && is_dir($dir)) {    i...

php实现图片上传、剪切功能

本文实例为大家详细介绍了php实现图片上传、剪切功能的具体代码,供大家参考,具体内容如下 <?php defined('BASEPATH') OR exit('No d...

phpwind中的数据库操作类

<?php /*来源:phpwind.net*/ Class DB { var $query_num = 0; function&...

PHP开发需要注意的安全问题

作为PHP程序员,特别是新手,对于互联网的险恶总是知道的太少,对于外部的入侵有很多时候是素手无策的,他们根本不知道黑客是如何入侵的、提交入侵、上传漏洞、sql 注入、跨脚本攻击等等。作为...