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 $string_to_search = "jb51.net...

php下一个阿拉伯数字转中文数字的函数

<?php function ch_num($num,$mode=true) { $char = array("零","壹","贰","叁"...

PHP内置过滤器FILTER使用实例

在这一章节里, 我们来了解一个不太常用但功能强大的 PHP 特性: FILTERS, 该扩展可以用来验证(validation)和纠错(sanitization)   当数据源...

php连接oracle数据库的核心步骤

本文实例讲述了php连接oracle数据库的核心步骤。分享给大家供大家参考,具体如下: 1、修改php.ini文件,打开extension=php_oci8.dll扩展。 2、拷贝php...

通过php动态传数据到highcharts

通过php动态传数据到highcharts

1:在平时工作中,在对数据进行展示的时候,是直接通过后台提供的接口来获取json串,用来展示。今天别人问怎么在本地演示一下请求的动态数据。 2:在本地搭建环境,我用的WampServer...