Php traversal CSV method php traversal csv class
- /**
- * Traverse csv files
- * Edit: bbs.it-home.org
- */
- 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
- ?>
|