Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
A 'pretty good' CSV library for Php
PHP
branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
README.md
pgcsv.php
test.php

README.md

pgcsv

A 'pretty good' CSV library for Php

Writing


$fields = array('name', 'phone', 'address');

$csv = new CSV('output.csv', $fields);

$person = array(
  'name' => 'Jow Brown',
  'phone' => '123 222-0123',
  'address' => '666 Buckingham Palace'
);

$csv->save($person);
$csv->close();

By default, CSV saves data with a utf-8 BOM to make it Excel-friendly. You can change that though.

Reading


$data = CSV::read('input.csv');

CSV expects the first row to be headers, but you can change that. You can also change the delimiter.

Iterating


foreach(CSV::iterate('input.csv') as $row){
  echo $row['name'] . "\n";
}

Iterating this way means the whole file does not need to be in memory.

CSV Generation


header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=file.csv");

$fields = array('name', 'phone', 'address');

$csv = new CSV('php://output', $fields);

$person = array(
  'name' => 'Jow Brown',
  'phone' => '123 222-0123',
  'address' => '666 Buckingham Palace'
);

$csv->save($person);
$csv->close();
Something went wrong with that request. Please try again.