php遍历CSV类实例

yipeiwu_com5年前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 session_start()问题解疑(详细介绍)

本文,将这些问题,做一个简单的汇总,以便大家查阅。1.错误提示Warning: Cannot send session cookie - headers already sentWarn...

PHP使用PHPexcel导入导出数据的方法

本文实例讲述了PHP使用PHPexcel导入导出数据的方法。分享给大家供大家参考,具体如下: 导入数据: <?php error_reporting(E_ALL); /...

php google或baidu分页代码

复制代码 代码如下:<?php /** 作者:潇湘博客 时间: 2009-11-26 php技术群: 37304662 使用方法: include_once'Pager.class...

让PHP支持页面回退的两种方法

在开发过程中,往往因为表单出错而返回页面的时候填写的信息都不见了,为了支持页面回跳,可以通过两种方法实现。 第一,使用Header方法设置消息头Cache-control header(...

PHP为表单获取的URL 地址预设 http 字符串函数代码

复制代码 代码如下: if (!preg_match("/^(http|ftp):/", $_POST['url'])) { $_POST['url'] = 'http://'.$_PO...