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默认设置中的Notice警告的方法

PHP的默认设置是显示Notice警告提示,这会造成页面无法正常显示出来。你有没定义的变量直接使用了。不过编PHP的时候本来就不像C++那么严格,编程的时候经常还会利用这个特点。 在把自...

pdo中使用参数化查询sql

方法 bindParam() 和 bindValue() 非常相似。 唯一的区别就是前者使用一个PHP变量绑定参数,而后者使用一个值。 所以使用bindParam是第二个参数只能用变量名...

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

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

PHP下载生成的csv文件及问题总结

最近做了一个项目需要把订单的信息显示出来,并且能够把相关信息放到一个.csv 文件中,下载到浏览器。虽然说csv是一种比较简单的excel表格形式,生成只要按指定格式然后生成.csv文件...

PHP 获取ip地址代码汇总

代码一: function getip() { static $ip = ''; $ip = $_SERVER['REMOTE_ADDR']; if(isset($_SE...