Class CSVParser
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:
$parser = new CSVParser('myfile.csv'); $parser->mapColumns( 'first name' => 'FirstName' 'lastname' => 'Surname', 'last name' => 'Surname' )); foreach($parser as $row) { // $row is a map of column name => column value $obj = new MyDataObject(); $obj->update($row); $obj->write(); }
Methods summary
public
|
#
__construct( mixed $filename, mixed $delimiter = ",", mixed $enclosure = '"' )
Open a CSV file for parsing. You can use the object returned in a foreach loop to extract the data |
public
|
#
mapColumns( mixed $columnMap )
Re-map columns in the CSV file. This can be useful for identifying synonyms in the file For example: $csv->mapColumns(array( 'firstname' => 'FirstName', 'last name' => 'Surname', )); |
public
|
#
provideHeaderRow( mixed $headerRow )
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. |
protected
|
|
protected
|
|
protected
|
|
protected
|
|
protected
|
Methods inherited from Object
__call(),
__toString(),
__wakeup(),
addMethodsFrom(),
addStaticVars(),
addWrapperMethod(),
add_extension(),
add_static_var(),
allMethodNames(),
cacheToFile(),
cacheToFileWithArgs(),
clearCache(),
combined_static(),
create(),
createMethod(),
create_from_string(),
defineMethods(),
exists(),
extInstance(),
extend(),
getCustomClass(),
getExtensionInstance(),
getExtensionInstances(),
get_extensions(),
get_static(),
hasExtension(),
hasMethod(),
has_extension(),
invokeWithExtensions(),
is_a(),
loadCache(),
parentClass(),
parse_class_spec(),
remove_extension(),
sanitiseCachename(),
saveCache(),
set_stat(),
set_static(),
set_uninherited(),
stat(),
strong_create(),
uninherited(),
uninherited_static(),
useCustomClass()
Methods inherited from Iterator
current(),
key(),
next(),
rewind(),
valid()
Magic methods summary
Properties summary
protected
mixed
|
$filename | |
protected
mixed
|
$fileHandle | |
protected
array
|
$columnMap |
#
Map of source columns to output columns Once they get into this variable, all of the source columns are in lowercase |
protected
mixed
|
$headerRow |
#
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 |
protected
mixed
|
$providedHeaderRow |
#
A custom header row provided by the caller |
protected
mixed
|
$currentRow |
#
The data of the current row |
protected
integer
|
$rowNum |
#
The current row number 1 is the first data row in the CSV file; the header row, if it exists, is ignored |
protected
string
|
$delimiter |
#
The character for separating columns |
protected
string
|
$enclosure |
#
The character for quoting colums |