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将整数数字转换为罗马数字实例分享

方法一:自定义函数 我们可以自己手动编写一个函数来实现此功能,这个函数可以将数字作为第一个参数,将其转换为罗马并返回。 注:大多数算法只能在1-4999的范围内工作,如果使用特大数,脚本...

HTML5读取本地文件 本地预览图片 视频等

HTML5为我们提供了一种与本地文件系统交互的标准方式:File Api。该规范主要定义了以下数据结构:FileFileListBlobHTML5访问本地文件系统时,需要先获取File对象句柄,怎么获...

PHP实现限制IP访问的方法

本文实例讲述了PHP实现限制IP访问的方法。分享给大家供大家参考,具体如下: //获取客户端ip if (getenv("HTTP_CLIENT_IP")) $ip = gete...

在命令行下运行PHP脚本[带参数]的方法

创建一个简单的文本文件,其中包含有以下PHP代码,并把它保存为hello.php: 复制代码 代码如下: <?php echo "Hello from the CLI"; ?>...

PHP 日,周,月点击排行统计

复制代码 代码如下: $now=time(); //当前时间 $StrUpdate = "Update $tbl_article set hits=hits+1"; if(date("d...