parseCSV

- parseCSV is an easy to use PHP class to read and write CSV data properly.
- Download parseCSV 0.4.3 beta.

Features

• Supports enclosed values, enclosed commas, double quotes and new lines.
• Automatic delimiter character detection.
• Sort data by specific fields/columns.
• Easy data manipulation.
• Basic SQL-like conditions, offset and limit options for filtering data.
• Store CSV data into an array, convert array to CSV data.
• Error detection for incorrectly formatted input. It attempts to be intelligent, but can not be trusted 100% due to the structure of CSV, and how different programs like Excel for example outputs CSV data.
• Support for character encoding conversion using PHP's iconv function (requires PHP 5).

Examples
- These examples use this CSV, saved in "books.csv":
rating,title,author,type,asin,tags,review
0,The Third Secret,Steve Berry,Book,0340899263,,need to find time to read this book
3,The Last Templar,Raymond Khoury,Book,0752880705,,
4,The Da Vinci Code (Hardcover),Dan Brown,"     Book     ",0385504209,book movie danbrown bestseller davinci,

1. Read a CSV file (with auto-detect delimiter character) and store data into an array:
<?php
// include parseCSV class
include('parsecsv/parsecsv.class.php');

// create new parseCSV object
$csv = new parseCSV();

// Parse 'books.csv' using automatic delimiter detection
$csv->auto('books.csv');

// if you know the delimiter, set the delimiter character (if not the default comma)
// $csv->delimiter = "\t";         // tab delimited

$csvdata = $csv->data;               // gets csv data into an array

// output result
echo '<pre>';
print_r($csvdata);
echo '</pre>';
?>

Result:
Array
(
  [0] => Array
    (
      [rating] => 0
      [title] => The Third Secret
      [author] => Steve Berry
      [type] => Book
      [asin] => 0340899263
      [tags] => 
      [review] => need to find time to read this book
    )
  [1] => Array
    (
      [rating] => 3
      [title] => The Last Templar
      [author] => Raymond Khoury
      [type] => Book
      [asin] => 0752880705
      [tags] => 
      [review] => 
    )
  [2] => Array
    (
      [rating] => 4
      [title] => The Da Vinci Code (Hardcover)
      [author] => Dan Brown
      [type] =>    Book   
      [asin] => 0385504209
      [tags] => book movie danbrown bestseller davinci
      [review] => 
    )
)

2. Read the CSV file, sorting data by "title".
<?php
// include parseCSV class.
include('parsecsv/parsecsv.class.php');

// create new parseCSV object
$csv = new parseCSV();

$csv->sort_by = 'title';

// Parse 'books.csv' using automatic delimiter detection...
$csv->auto('books.csv');

$csvdata = $csv->data;               // get csv data into an array

// output result
echo '<pre>';
print_r($csvdata);
echo '</pre>';
?>

Result:
Array
(
  [The Da Vinci Code (Hardcover)] => Array
    (
      [rating] => 4
      [title] => The Da Vinci Code (Hardcover)
      [author] => Dan Brown
      [type] =>    Book   
      [asin] => 0385504209
      [tags] => book movie danbrown bestseller davinci
      [review] => 
    )
  [The Last Templar] => Array
    (
      [rating] => 3
      [title] => The Last Templar
      [author] => Raymond Khoury
      [type] => Book
      [asin] => 0752880705
      [tags] => 
      [review] => 
    )
  [The Third Secret] => Array
    (
      [rating] => 0
      [title] => The Third Secret
      [author] => Steve Berry
      [type] => Book
      [asin] => 0340899263
      [tags] => 
      [review] => need to find time to read this book
    )
)

3. Convert 2D array to CSV, and send headers to browser to treat output as a file and download it.
<?php
// include parseCSV class.
include('parsecsv/parsecsv.class.php');

$csvHeader = array('category', 'link');              // csv header

// 2 dimensional array with data for csv (according to header)
$csvData = array(
  array('tutorials', 'http://coursesweb.net/php-mysql/tutorials_t'),
  array('scripts', 'http://coursesweb.net/php-mysql/scripts_s2'),
  array('resources', 'http://coursesweb.net/php-mysql/download_l2')
 );

// create new parseCSV object
$csv = new parseCSV();

// convert 2D array to csv data and output as a file to download it
// (filename, csv-data, csv-header, delimiter)
$csv->output('coursesweb.csv', $csvData, $csvHeader, ',');
?>

Result this CSV:
category,link
tutorials,http://coursesweb.net/php-mysql/tutorials_t
scripts,http://coursesweb.net/php-mysql/scripts_s2
resources,http://coursesweb.net/php-mysql/download_l2

- In the archive with parseCSV class you'll find other examples too.
parseCSV Web Site.