- 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.

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which attribute is used in <img> tag for the address of the image?
href src rel
<img src="http://coursesweb.net/imgs/webcourses.gif" width="191" height="63" alt="Courses-Web" />
Which CSS code hides the element on page?
display: none; display: inline; position: relative;
#id {
  display: none;
}
What instruction stops the execution of a while() or for() statement?
continue prompt() break
for(var i = 0; i< 8; i++) {
  if(i > 1) break;
  alert(i);
}
Indicate the function that can create a constant.
define() include() defined()
define("CONSTANT_NAME", "value");
echo CONSTANT_NAME;
parseCSV

Last accessed pages

  1. Get the Height and Width of web Page and browser Window (1007)
  2. Bind Tool and Control Points (1840)
  3. Register and show online users and visitors (9202)
  4. Difference between two Dates - Time and Date (630)
  5. Add, Change, and Remove Attributes with jQuery (18463)

Top accessed pages

  1. Courses Web: PHP-MySQL JavaScript Ajax HTML CSS Flash-AS3 (33927)
  2. PHP-MySQL free course, online tutorials PHP MySQL code (27392)
  3. Get Attribute (ID, Class, Name, Title, Src) with jQuery (25928)
  4. Read Excel file data in PHP - PhpExcelReader (25868)
  5. PHP PDO - exec (INSERT, UPDATE, DELETE) MySQL (22673)