php遍历CSV类实例

yipeiwu_com5年前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 实用代码收集

1. 可阅读随机字符串 此代码将创建一个可阅读的字符串,使其更接近词典中的单词,实用且具有密码验证功能。 复制代码 代码如下: /************** *@length - le...

php用正则判断是否为数字的方法

前两天朋友的一个网站上有人利用php注入提交flash游戏分数,后来找原因才发现是有一位参数没有做数字判断导致。 本来保存游戏分数是 game.php?ac=save&fgid=...

thinkphp使用phpmailer发送邮件的方法

本文实例讲述了thinkphp使用phpmailer发送邮件的方法。分享给大家供大家参考。具体分析如下: phpmailer发送邮件是php开发者首选的一个邮件发送插件了,下面我来介绍怎...

php 检查电子邮件函数(自写)

复制代码 代码如下: function is_valid_email_address($email){ $qtext = '[^//x0d//x22//x5c//x80-//xff]';...

解析php中curl_multi的应用

相信许多人对php手册中语焉不详的curl_multi一族的函数头疼不已,它们文档少,给的例子 更是简单的让你无从借鉴,我也曾经找了许多网页,都没见一个完整的应用例子。•cu...