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将HTML转换成文本的实现代码

核心代码: <?php // $document 应包含一个 HTML 文档。 // 本例将去掉 HTML 标记,javascript 代码 // 和空白字符。还会将一...

PHP防止注入攻击实例分析

本文以实例形式详细分析了PHP防止注入攻击的方法。分享给大家供大家参考。具体分析如下: PHP addslashes() 函数--单撇号加斜线转义 PHP String 函数 定义和用法...

PHP 冒泡排序算法的实现代码

基本概念 冒泡排序的基本概念是:依次比较相邻的两个数,将小数放在前面,大数放在后面。即首先比较第1 个和第2个数,将小数放前,大数放后。然后比较第2个数和第3个数,将小数放前,大数放后,...

PHP使用SOAP调用API操作示例

本文实例讲述了PHP使用SOAP调用API操作。分享给大家供大家参考,具体如下: /*图片转换为 base64格式编码*/ function base64EncodeImage($i...

PHP使用PDO访问oracle数据库的步骤详解

前言 PDO 从一开始就吸取了现有数据库扩展成功和失败的经验教训。因为 PDO 的代码是全新的,所以我们有机会重新开始设计性能,以利用 PHP 5 的最新特性。 PDO 旨在将常见的数...