note 86624 added to function.fgetcsv

From: Date: Mon, 27 Oct 2008 01:51:49 +0000
Subject: note 86624 added to function.fgetcsv
Groups: php.notes 
Request: Send a blank email to php-notes+get-145787@lists.php.net to get a copy of this message
In my opinion, the two examples that use the SPL Iterator Interface don't work as they should. If you call several times to the method current() then it eventually will reach the eof... <?php public function current() { $this->currentElement = fgetcsv($this->filePointer, self::ROW_SIZE, $this->delimiter); $this->rowCounter++; return $this->currentElement; } ?> If you want to go next, you need to call the method next(). So here is my solution... <?php /** * @author cizar@nixar.org */ class CsvIterator implements Iterator { const DEFAULT_DELIMITER = ';'; const DEFAULT_LENGTH = 4096; /** * The CSV file handler. * * @var resource * @access private */ private $_handler = null; /** * The delimiter of the CSV file. * * @var string * @access private */ private $_delimiter = null; /** * The dafs * * @var integer * @access private */ private $_length = null; /** * The row counter. * * @var integer * @access private */ private $_key = null; /** * The element that will be returned on each iteration. * * @var mixed * @access private */ private $_current = null; /** * This is the constructor. It try to open the CSV file. * * @access public * @param string $filename The fullpath of the CSV file. * @param string $delimiter The delimiter. * @param integer $length The amount of bytes to be read on each iteration. * * @throws Exception */ public function __construct ($filename, $delimiter = self::DEFAULT_DELIMITER, $length = self::DEFAULT_LENGTH) { if (($this->_handler = fopen($filename, 'r')) === false) { throw new Exception("The file '$filename' cannot be opened"); } $this->_delimiter = $delimiter; $this->_length = $length; } /** * This is the destructor. It close the CSV file. * * @access public */ public function __destruct () { fclose($this->_handler); } /** * This method move the file pointer to the next row. * * @access public */ public function next () { $this->_read(); $this->_key += 1; } /** * This method reset the file handler. * * @access public */ public function rewind () { rewind($this->_handler); $this->_read(); $this->_key = 0; } /** * This method returns the current row number. * * @access public */ public function key () { return $this->_key; } /** * This methods return the current CSV row data. * * @access public * @return array The row as an one-dimensional array */ public function current () { return $this->_current; } /** * This method checks if the current row is readable. * * @access public * @return boolean If the current row is readable. */ public function valid () { return $this->_current !== FALSE; } /** * This method read the next row of the CSV file. * * @access private */ private function _read () { $this->_current = fgetcsv($this->_handler, $this->_length, $this->_delimiter); } } ---- Server IP: 190.2.55.3 Probable Submitter: 190.48.99.214 ---- Manual Page -- http://www.php.net/manual/en/function.fgetcsv.php Edit -- https://master.php.net/note/edit/86624 Del: integrated -- https://master.php.net/note/delete/86624/integrated Del: useless -- https://master.php.net/note/delete/86624/useless Del: bad code -- https://master.php.net/note/delete/86624/bad+code Del: spam -- https://master.php.net/note/delete/86624/spam Del: non-english -- https://master.php.net/note/delete/86624/non-english Del: in docs -- https://master.php.net/note/delete/86624/in+docs Del: other reasons-- https://master.php.net/note/delete/86624 Reject -- https://master.php.net/note/reject/86624 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#145787) next »