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自带了方法做校验。 filter_var filter_var是PHP内置的一个变量过滤的方法,...

php排序算法实例分析

php排序算法实例分析

本文实例分析了php排序算法。分享给大家供大家参考,具体如下: 用PHP写排序,虽然PHP自动了很多排序方式,SQL语句也可以很快速的从数据库里有序的读出数据。但是不同的需求还有灵活 运...

一篇入门的php Class 文章

刚在大略浏览了一下首页更新的那篇有关Class的文章(指PHPE的那篇 http://www.phpe.net/articles/389.shtml ),很不错,建议...

php生成zip压缩文件的方法详解

复制代码 代码如下:require_once "./include/zip.php"; $zip = new PHPZip(); //$zip -> createZip("要压缩的...

采用header定义为文件然后readfile下载(隐藏下载地址)

复制代码 代码如下:<?php function sendFile($fileName, $fancyName = '', $forceDownload = true, $spee...