note 78228 added to function.fgetcsv
| From: | marcus at synchromedia dot co dot uk | Date: | Wed, 03 Oct 2007 16:44:46 +0000 |
| Subject: | note 78228 added to function.fgetcsv | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-131044@lists.php.net to get a copy of this message | ||
This is a minor fix to mortanon@gmail.com's CSVIterator. The original version would die if the
last line of a file did not end in a line break and you called valid() inside the iterator loop
because the file would have already been closed and thus feof() would have an invalid file pointer
param.
<?php
/**
* @author mortanon@gmail.com
* @link http://uk.php.net/manual/en/function.fgetcsv.php
*/
class CsvIterator implements Iterator {
const ROW_SIZE = 4096;
/**
* The pointer to the cvs file.
* @var resource
* @access private
*/
private $filePointer = NULL;
/**
* The current element, which will
* be returned on each iteration.
* @var array
* @access private
*/
private $currentElement = NULL;
/**
* The row counter.
* @var int
* @access private
*/
private $rowCounter = NULL;
/**
* The delimiter for the csv file.
* @var str
* @access private
*/
private $delimiter = NULL;
/**
* This is the constructor.It try to open the csv file.The method throws an exception
* on failure.
*
* @access public
* @param str $file The csv file.
* @param str $delimiter The delimiter.
*
* @throws Exception
*/
public function __construct($file, $delimiter=',') {
try {
$this->filePointer = fopen($file, 'r');
$this->delimiter = $delimiter;
}
catch (Exception $e) {
throw new Exception('The file "'.$file.'" cannot be read.');
}
}
/**
* This method resets the file pointer.
*
* @access public
*/
public function rewind() {
$this->rowCounter = 0;
rewind($this->filePointer);
}
/**
* This method returns the current csv row as a 2 dimensional array
*
* @access public
* @return array The current csv row as a 2 dimensional array
*/
public function current() {
$this->currentElement = fgetcsv($this->filePointer, self::ROW_SIZE, $this->delimiter);
$this->rowCounter++;
return $this->currentElement;
}
/**
* This method returns the current row number.
*
* @access public
* @return int The current row number
*/
public function key() {
return $this->rowCounter;
}
/**
* This method checks if the end of file is reached.
*
* @access public
* @return boolean Returns true on EOF reached, false otherwise.
*/
public function next() {
if (is_resource($this->filePointer)) {
return !feof($this->filePointer);
}
return false;
}
/**
* This method checks if the next row is a valid row.
*
* @access public
* @return boolean If the next row is a valid row.
*/
public function valid() {
if (!$this->next()) {
if (is_resource($this->filePointer)) {
fclose($this->filePointer);
}
return false;
}
return true;
}
}
?>
----
Server IP: 194.145.210.3
Probable Submitter: 87.74.30.27
----
Manual Page -- http://www.php.net/manual/en/function.fgetcsv.php
Edit -- https://master.php.net/note/edit/78228
Del: integrated -- https://master.php.net/note/delete/78228/integrated
Del: useless -- https://master.php.net/note/delete/78228/useless
Del: bad code -- https://master.php.net/note/delete/78228/bad+code
Del: spam -- https://master.php.net/note/delete/78228/spam
Del: non-english -- https://master.php.net/note/delete/78228/non-english
Del: in docs -- https://master.php.net/note/delete/78228/in+docs
Del: other reasons-- https://master.php.net/note/delete/78228
Reject -- https://master.php.net/note/reject/78228
Search -- https://master.php.net/manage/user-notes.php