, (*1)
PHP CSV Parser is a simple csv parser written in php. It can parse csv strings or files., (*2)
It utilises Laravel's Support Library to help with providing a simple and clean structure., (*3)
Contents
Installation
PHP CSV Parser is available as a composer package and can be added to your composer.json like so., (*4)
"require": {
...
"jralph/phpcsvparser": "2.*"
...
}
Usage
Using the CSV parser is simple and can be done by directly accessing the Jralph\PHPCSVParser\ParserManager class, or either of the Parser classes (Jralph\PHPCSVParser\Parsers\StringParser or Jralph\PHPCSVParser\Parsers\FileParser)., (*5)
For ease of use, there is also a facade that has been created. Jralph\PHPCSVParser\Facades\Parser. This facade can call the create method on the ParserManager object., (*6)
create('csv string of path to file here.');
// Using the File Parser (NO AUTO DETECTION)
$parser = new FileParser('path/to/file/here');
// Using the String Parser (NO AUTO DETECTION)
$parser = new StringParser('"csv","string","here"');
// Once you have an instance of a parser, you can parse the csv.
// You can optionally pass in any of the paramaters below.
$csv = $parser->parse($delimiter = ',', $enclosure = '"', $escape = '\\');
// Or no paramaters at all.
$csv = $parser->parse();
// Maybe your csv does not have column headings.
$csv = $parser->withoutHeadings()->parse();
?>
Working with the CSV
Once you have parsed the CSV, you will be given a CSV object. This object contains a collection of headers (if any) and a collection of all of the csv rows., (*7)
parse();
foreach ($csv->rows() as $row) {
// Do something with the rows.
}
?>
CSV Rows
When looping through csv rows, each row is returned as an instance of the CSVRow object., (*8)
You can access data through this object in many ways., (*9)
parse();
foreach ($csv->rows() as $row) {
// We have not told the parser to process without headings, so we can access
// each row by its heading name.
echo $row->heading1; // As an object.
echo $row['heading2']; // As an array.
echo $row->getRow('heading3'); // By a method.
}
// If we do not have or know the headings, we can loop through the row attributes.
foreach ($csv->rows() as $row) {
// Loop through the $row object.
foreach ($row as $column) {
echo $column;
}
// Get an array of all attributes.
$attributes = $row->getAttributes();
}
?>
Arrays and JSON
The following objects contain convertors to convert the object to an array and to json., (*10)
parse();
echo $csv->toJson(); // Echo the entire csv, headings and rows, as json.
foreach ($csv->rows() as $row) {
echo $row->toJson(); // Echo the entire row as json.
}
?>