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 park、unpark、ord 函数使用方法(二进制流接口应用实例)

php park、unpark、ord 函数使用方法(二进制流接口应用实例)

park,unpark,ord这3个函数,在我们工作中,用到它们的估计不多。 我在最近一个工作中,因为通讯需要用到二进制流,然后接口用php接收。当时在处理时候,查阅不少资料。因为它们使...

PHP实现的折半查询算法示例

PHP实现的折半查询算法示例

本文实例讲述了PHP实现的折半查询算法。分享给大家供大家参考,具体如下: 什么是折半查询算法?具体文字描述自己百度。直接上代码: <?php header("Conte...

输入值/表单提交参数过滤有效防止sql注入的方法

输入值/表单提交参数过滤,防止sql注入或非法攻击的方法: 复制代码 代码如下: /** * 过滤sql与php文件操作的关键字 * @param string $string * @r...

PHP乱码问题,UTF-8乱码常见问题小结

一.HTML页面转UTF-8编码问题 1.在head后,title前加入一行: <meta http-equiv='Content-Type' content='text/html...

关于PHP通用返回值设置方法

遇到一个不错的php代码。记录一下。 在写php代码时,经常会遇到需要返回值的情况,可以统一设置一下返回值的格式。 下面就是一个不错的例子。 配置类Return.conf.php &...