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+ajax无刷新分页实例详解

本文实例讲述了php+ajax无刷新分页实现方法。分享给大家供大家参考,具体如下: ajax_page_show_userinfo.php页面如下: <meta 'Conten...

php递归实现无限分类的方法

本文实例讲述了php递归实现无限分类的方法。分享给大家供大家参考。具体如下: <?php $rows = array( array( 'id' =>...

高质量PHP代码的50个实用技巧必备(下)

接着上篇《高质量PHP代码的50个实用技巧必备(上)》继续研究。 26. 避免直接写SQL, 抽象之 不厌其烦的写了太多如下的语句: <span style="color...

php UTF8 文件的签名问题

也就是 有BOM 格式编码,或者 无BOM格式编码。 如果看文件的内容,是看不出任何差别的,以下列文件(schema.sqlite.sql)内容为例: schema.sqlite.sql...

PHP中的print_r 与 var_dump 输出数组

print_r() 和 var_dump() 函数可以打印输出整个数组内容及结构。 print_r() 利用 print_r() 函数可以打印输出整个数组内容及结构,按照一定格式显示键和...