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 500报错的快速解决方法

1 先看nginx error.log 指定的错误日记文件路径 找到这个日记文件看 里面信息 2 再看  php-fpm.conf 里面指定的PHP错误日记的路径 具体如下 p...

php一个解析字符串排列数组的方法

本文实例讲述了php一个解析字符串排列数组的方法。分享给大家供大家参考。具体如下: <?php $str="1|苹果|30 1|桃子|50 1|普通|10 2|小麦|1...

用PHP实现小写金额转换大写金额的代码(精确到分)

复制代码 代码如下: /** *数字金额转换成中文大写金额的函数 *String Int $num 要转换的小写数字或小写字符串 *return 大写字母 *小数位为两位 **/ fun...

PHP进程通信基础之信号

使用信号通信。可以使用kill -l 来查看当前系统的信号类型。 每个信号所代表的的详细含义,请查看我的这篇文章:https://www.jb51.net/article/106040...

php可生成缩略图的文件上传类实例

本文实例讲述了php可生成缩略图的文件上传类及其用法。分享给大家供大家参考。具体实现方法如下: 类文件调用方法如下: 复制代码 代码如下:<?php if ($_GET[...