Comments: on this page. Click to read or post your own.

Class CSVParser

Description

Implements interfaces:

  • Iterator (internal interface)

Class to handle parsing of CSV files, where the column headers are in the first row.

The idea is that you pass it another object to handle the actual procesing of the data in the CSV file.

Usage:

  1.  $parser new CSVParser('myfile.csv');
  2.  $parser->mapColumns(
  3.     'first name' => 'FirstName'
  4.     'lastname' => 'Surname',
  5.     'last name' => 'Surname'
  6.  ));
  7.  foreach($parser as $row{
  8.       // $row is a map of column name => column value
  9.    $obj new MyDataObject();
  10.    $obj->update($row);
  11.    $obj->write();
  12.  }

Located in /sapphire/dev/CSVParser.php (line 23)

Object
   |
   --CSVParser
Variable Summary
mixed $columnMap
mixed $currentRow
mixed $delimiter
mixed $enclosure
mixed $fileHandle
mixed $filename
mixed $headerRow
mixed $rowNum
Method Summary
CSVParser __construct ($filename $filename, [$delimiter $delimiter = ","], [$enclosure $enclosure = '"'])
void closeFile ()
void fetchCSVHeader ()
void fetchCSVRow ()
void mapColumns ( $columnMap)
void openFile ()
void provideHeaderRow ( $headerRow)
void remapHeader ( $header)
Variables
mixed $columnMap = array() (line 31)

Map of source columns to output columns

Once they get into this variable, all of the source columns are in lowercase

  • access: protected
mixed $currentRow = null (line 47)

The data of the current row

  • access: protected
mixed $delimiter = "," (line 58)

The character for separating columns

  • access: protected
mixed $enclosure = '"' (line 63)

The character for quoting colums

  • access: protected
mixed $fileHandle (line 25)
  • access: protected
mixed $filename (line 24)
  • access: protected
mixed $headerRow = null (line 37)

The header row used to map data in the CSV file

To begin with, this is null. Once it has been set, data will get returned from the CSV file

  • access: protected
mixed $providedHeaderRow = null (line 42)

A custom header row provided by the caller

  • access: protected
mixed $rowNum = 0 (line 53)

The current row number

  1. is the first data row in the CSV file; the header row, if it exists, is ignored

  • access: protected

Inherited Variables

Inherited from Object

Object::$builtInMethods
Object::$class
Object::$classConstructed
Object::$extensions
Object::$extension_instances
Object::$extraMethods
Object::$extraStatics
Object::$statics
Object::$static_cached
Methods
Constructor __construct (line 72)

Open a CSV file for parsing.

You can use the object returned in a foreach loop to extract the data

CSVParser __construct ($filename $filename, [$delimiter $delimiter = ","], [$enclosure $enclosure = '"'])
  • $filename $filename: The name of the file. If relative, it will be relative to the site's base dir
  • $delimiter $delimiter: The character for seperating columns
  • $enclosure $enclosure: The character for quoting or enclosing columns

Redefinition of:
Object::__construct()
closeFile (line 124)

Close the CSV file and re-set all of the internal variables

  • access: protected
void closeFile ()
fetchCSVHeader (line 137)

Get a header row from the CSV file

  • access: protected
void fetchCSVHeader ()
fetchCSVRow (line 157)

Get a row from the CSV file and update $this->currentRow;

  • access: protected
void fetchCSVRow ()
mapColumns (line 90)

Re-map columns in the CSV file.

This can be useful for identifying synonyms in the file For example:

  1.  $csv->mapColumns(array(
  2.    'firstname' => 'FirstName',
  3.    'last name' => 'Surname',
  4.  ));

void mapColumns ( $columnMap)
  • $columnMap
openFile (line 111)

Open the CSV file for reading

  • access: protected
void openFile ()
provideHeaderRow (line 104)

If your CSV file doesn't have a header row, then you can call this function to provide one.

If you call this function, then the first row of the CSV will be included in the data returned.

void provideHeaderRow ( $headerRow)
  • $headerRow
remapHeader (line 145)

Map the contents of a header array using $this->mappedColumns

  • access: protected
void remapHeader ( $header)
  • $header

Inherited Methods

Inherited From Object

Object::__construct()
Object::addMethodsFrom()
Object::addStaticVars()
Object::addWrapperMethod()
Object::add_extension()
Object::allMethodNames()
Object::buildMethodList()
Object::cacheToFile()
Object::cacheToFileWithArgs()
Object::create()
Object::createMethod()
Object::defineMethods()
Object::exists()
Object::extend()
Object::extInstance()
Object::getCustomClass()
Object::hasExtension()
Object::hasMethod()
Object::invokeWithExtensions()
Object::is_a()
Object::loadCache()
Object::parentClass()
Object::sanitiseCachename()
Object::saveCache()
Object::set_stat()
Object::set_uninherited()
Object::stat()
Object::strong_create()
Object::uninherited()
Object::useCustomClass()
Object::__call()
Object::__toString()
blog comments powered by Disqus

Documentation generated on Sun, 19 Oct 2008 06:39:35 +1300 by phpDocumentor 1.3.2