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

WBOY
发布: 2016-07-25 08:56:15
原创
1015 人浏览过
  1. /**
  2. * 遍历csv文件
  3. * edit: bbs.it-home.org
  4. */
  5. class CSVIterator implements Iterator
  6. {
  7. const ROW_SIZE = 4096;
  8. private $filePointer;
  9. private $currentElement;
  10. private $rowCounter;
  11. private $delimiter;
  12. public function __construct( $file, $delimiter = ',' )
  13. {
  14. $this->filePointer = fopen( $file, 'r' );
  15. $this->delimiter = $delimiter;
  16. }
  17. public function rewind()
  18. {
  19. $this->rowCounter = 0;
  20. rewind( $this->filePointer );
  21. }
  22. public function current()
  23. {
  24. $this->currentElement = fgetcsv( $this->filePointer, self::ROW_SIZE, $this->delimiter );
  25. $this->rowCounter++;
  26. return $this->currentElement;
  27. }
  28. public function key()
  29. {
  30. return $this->rowCounter;
  31. }
  32. public function next()
  33. {
  34. return !feof( $this->filePointer );
  35. }
  36. public function valid()
  37. {
  38. if( !$this->next() )
  39. {
  40. fclose( $this->filePointer );
  41. return FALSE;
  42. }
  43. return TRUE;
  44. }
  45. } // end class
  46. ?>
复制代码


来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板