教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 php遍历CSV的方法 php遍历csv的类

php遍历CSV的方法 php遍历csv的类

发布时间:2017-01-10   编辑:jiaochengji.com
本文分享一例php遍历csv文件的代码,实现一个自定义的php类,来遍历csv文件中的内容,有需要的朋友参考下吧。

php遍历csv文件内容,代码:
 

复制代码 代码示例:
<?php
/**
* 遍历csv文件
* edit: www.jbxue.com
*/
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遍历CSV的方法 php遍历csv的类
php无限遍历目录代码
解决CodeIgniter无法上传CSV文件问题
php导出csv时身份证号禁止科学计数
php导出csv格式文件的例子
php遍历目录与其下所有文件
php读取csv、写入csv与导出csv文件
php读取csv时,读取中文乱码问题解决方法
php读取(打开)csv文件的小例子
php遍历数组之list foreach each用法总结

关键词: php csv   
[关闭]
~ ~