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实现通过文本文件统计页面访问量功能。分享给大家供大家参考,具体如下: 一 代码 index.php <?php session_start(); i...

php校验表单检测字段是否为空的方法

本文实例讲述了php校验表单检测字段是否为空的方法。分享给大家供大家参考。具体如下: php校验表单,检测字段是否为空,当表单中有未填写的字段,则会显示错误信息。 <html&...

Yii 使用intervention/image拓展实现图像处理功能

一:安装intervention/image拓展 composer require intervention/image 二:上传文件 \Intervention\Image\Im...

PHP文章按日期(月日)SQL归档语句

复制代码 代码如下: select FROM_UNIXTIME(pubtime, '%Y-%m') as pubtime, count(*) as cnt from articles g...

thinkphp Apache配置重启Apache1 restart 出错解决办法

概要:   thinkphp 在Apache上配置启用伪静态,重启Apache1 restart 竟然失败了,报错     Job for httpd.service failed be...