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 空格,换行,跳格使用说明

首先说说\n,\r,\t \n 软回车: 在Windows 中表示换行且回到下一行的最开始位置 在Linux、unix 中只表示换行,但不会回到下一行的开始位置。 \r 软空格: 在Li...

PHP 5.0对象模型深度探索之属性和方法

可以联用->,如果一个对象的属性包含了一个对象,你可以使用两个->运算符来得到内部对象的属性. 你甚至可以用双重引用的字符串来放置这些表达式. 下面的例子中,对象Ho...

PHP标准库(PHP SPL)详解

PHP标准库(PHP SPL)详解

什么是SPL? SPL,PHP 标准库(Standard PHP Library) ,此从 PHP 5.0 起内置的组件和接口,并且从 PHP5.3 已逐渐的成熟。SPL 其实在所有的...

用PHP为SHOPEX增加日志功能代码

尤其像知道哪些蜘蛛对本站进行了访问,访问的频度,页面,普通的站点统计都是无法解决的。 虽然我对PHP了解的很少,但是凭借.NET的开发经验,借助百度仍然很快的完成了,虽然简单,大家莫笑。...

PHP4中session登录页面的应用

<?php //这个脚本是用来给用户输入口令,并判断口令是否正确的。 //如果正确则转到欢迎页面。 if ($login){    &nbs...