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实现的二叉树遍历算法示例

本文实例讲述了php实现的二叉树遍历算法。分享给大家供大家参考,具体如下: 今天使用php来实现二叉树的遍历 创建的二叉树如下图所示 php代码如下所示: <?php...

php+ajax实现无刷新的新闻留言系统

php+ajax实现无刷新的新闻留言系统

本文介绍了一款无刷新的新闻留言系统,最简明易懂的一个ajax无刷新留言系统,源码中省略了接受数据验证的过程,大家可根据自己的需求进行扩展,下面进入主题。 核心源码: 1.配置文件:co...

windows下PHP_intl.dll正确配置方法(apache2.2+php5.3.5)

配置php_intl模块总是加载失败,在这找到了解决方法http://stackoverflow.com/questions/1451468/php-intl-extension。 首先...

php下网站防IP攻击代码,超级实用

今天我开发了下面的代码,算是大功初成,一天拦截了15个IP,服务器负载正常。 复制代码 代码如下: <?php //查询禁止IP $ip =$_SERVER['REMOTE_ADD...

php写入数据到CSV文件的方法

本文实例讲述了php写入数据到CSV文件的方法。分享给大家供大家参考。具体实现方法如下: <?php $row = 0; ini_set('max_execution_...