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安全配置  一、Web服务器安全  PHP其实不过是Web服务器的一个模块功能,所以首先要保证Web服务器的安全。当然Web服务器要安全又必须是先保证系...

php之static静态属性与静态方法实例分析

本文实例讲述了php之static静态属性与静态方法。分享给大家供大家参考。具体如下: <?php /* * static */ /*静态:属于类而不属于单个对象...

php生成zip压缩文件的方法详解

复制代码 代码如下:require_once "./include/zip.php"; $zip = new PHPZip(); //$zip -> createZip("要压缩的...

验证坐标在某坐标区域内php代码

验证坐标在某坐标区域内php代码

之前碰到的这样一个需求,要将公司的服务在地图中显示出来,并将用户每天的访问坐标进行统计看有多少用户是在所能达到的服务范围半径内。 以下是PHP代码的实现 (仅验证坐标在某片坐标区域内)...

PHP生成指定随机字符串的简单实现方法

本文实例讲述了PHP生成指定随机字符串的简单实现方法。分享给大家供大家参考。具体分析如下: 这是一个简单的函数,没有对生成的内容作强制设定。所以在生成的字符串长度较少的时候,会出现没有指...