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会在双引号包围的字符串中搜寻变量,单引号则不会,注意:只有echo能这么做,它是一种可以把多个字符串当作参数的“函数”(译注:PHP手册中说echo是语言结构,不是真正的函数,...

php数组函数序列之rsort() - 对数组的元素值进行降序排序

rsort()定义和用法 rsort() 函数对数组的元素按照键值进行逆向排序。与 arsort() 的功能基本相同。 注释:该函数为 array 中的单元赋予新的键名。这将删除原有的键...

php使用simple_html_dom解析HTML示例

本文实例讲述了php使用simple_html_dom解析HTML的方法。分享给大家供大家参考,具体如下: 今天写了两个爬虫, 一个使用Python, 一个使用PHP, 说实在, 两个实...

PHP获取昨天、今天及明天日期的方法

本文实例讲述了PHP获取昨天、今天及明天日期的方法。分享给大家供大家参考,具体如下: //PHP返回昨天的日期 function get_last_date() { $tomorr...

解析php中heredoc的使用方法

Heredoc技术,在正规的PHP文档中和技术书籍中一般没有详细讲述,只是提到了这是一种Perl风格的字符串输出技术。但是现在的一些论坛程 序,和部分文章系统,都巧妙的使用heredoc...