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 导出excle的.csv格式的数据时乱码问题

完美解决php 导出excle的.csv格式的数据时乱码问题

1、header('Content-Encoding: XXXX'); 有可能是编码问题:可以尝试UTF-8,GBK,GB2312,等编码格式 2、有可能是文件编码问题,虽然UTF-8...

php禁用cookie后session设置方法分析

本文实例讲述了php禁用cookie后session设置方法。分享给大家供大家参考,具体如下: 我们都知道当在session 会话有基于cookie和基于url两种传递SESSIONID...

php将print_r处理后的数据还原为原始数组的解决方法

PHP print_r方法可以把变量打印显示,使变量易于理解。如果变量是string,integer或float,将打印变量值本身,如果变量是array,将会按照一定格式显示键和元素。o...

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

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

php设置编码格式的方法

 a. 如果欲使用gb2312编码,那么php要输出头:header(“Content-Type: text/html; charset=gb2312"),静态页面添加<meta...