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、对接口的使用是通过关键字implements。对抽象类的使用是通过关键字extends。当然...

PHP扩展mcrypt实现的AES加密功能示例

PHP扩展mcrypt实现的AES加密功能示例

本文实例讲述了PHP扩展mcrypt实现的AES加密功能。分享给大家供大家参考,具体如下: AES(Advanced Encryption Standard,高级加密标准)是美国联邦政府...

php用户名的密码加密更安全的方法

php用户名的密码加密更安全的方法

php中对用户密码的加密主要有两种方法,一种是利用md5加密,另一种是利用password_hash加密,两种方法中后一种的方法比前一种方法安全很多,几乎不能被黑客破解,但php版本必须...

PHP递归实现快速排序的方法示例

本文实例讲述了PHP递归实现快速排序的方法。分享给大家供大家参考,具体如下: 首先我们要理解一下快速排序的原理:找到当前数组中的任意一个元素(一般选择第一个元素),作为标准,新建两个空数...

php格式化时间戳显示友好的时间实现思路及代码

在项目中时间一律显示为2014-10-20 10:22显得很呆板。在微博、QQ空间等网站通常会显示为几秒前,几分钟前,几小时前等容易阅读的时间,我们称之为友好的时间格式。那么用php怎么...