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.ini上传相关配置 error错误号 单文...

PHP生成条形图的方法

PHP生成条形图的方法

本文实例讲述了PHP生成条形图的方法。分享给大家供大家参考。具体实现方法如下: 复制代码 代码如下: <?php   // create an array of...

PHP获取网页所有连接的方法(附demo源码下载)

本文实例讲述了PHP获取网页所有连接的方法。分享给大家供大家参考,具体如下: function getHtml($url, $charset='utf-8') { $curl =...

php 判断页面或图片是否经过gzip压缩的方法

使用php判断页面或图片是否经过gzip压缩方法 1.使用get_headers 页面内容 <?php ob_start('ob_gzhandler'); // 开启...

自己写的兼容低于PHP 5.5版本的array_column()函数

array_column 用于获取二维数组中的元素(PHP 5.5新增函数),但我们有时候需要在低版本的PHP环境中使用… if( ! function_exists('array_...