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程序设计有所帮助。

相关文章

在数据量大(超过10万)的情况下

这个方法不错。 那你大可以把默认值设为99999...

php获取开始与结束日期之间所有日期的方法

本文实例讲述了php获取开始与结束日期之间所有日期的方法。分享给大家供大家参考,具体如下: /** * 获取指定日期段内每一天的日期 * @param Date $startda...

PHP中strtr与str_replace函数运行性能简单测试示例

PHP中strtr与str_replace函数运行性能简单测试示例

本文实例讲述了PHP中strtr与str_replace函数运行性能简单测试。分享给大家供大家参考,具体如下: strtr与str_replace函数性能,很简单的一个测试,只是简单的测...

PHP学习笔记之三 数据库基本操作

下面是在Linux上登录mysql,创建数据库和创建表的过程。 yin@yin-Ubuntu10:~$ mysql -u root -p Enter password: Welcome...

phpcms的分类名称和类别名称的调用

话不多说,请看代码: //在需要调用的模板前写这句代码,$CATEGORYS 代表分类,$TYPE 代表类别(没有这句话,调不出来) {php $CATEGORYS = getcac...