Archive

Archive for May, 2010

CSV Export Class for PHP

Since I’ve been working with a lot of different data sources lately, I’ve found myself needing an easy to use CSV Export class. So, here it is, CSVMaker.php

This class makes it very easy to create CSV files on the fly, from any type of data. Here’s a usage example:

$CSV = new CSVMaker();
 
// Optionally Change the Format
$CSV->csvDelimeter    = ","; // (What seperates each value in a set Foo,Bar,Etc)
$CSV->csvLine         = "\n"; // (What ends one set of data - usually a new line)
$CSV->csvCapsule      = '"'; // (What comes before and after each piece of data "Foo","Bar","Etc")
 
$CSVHeader = array();
$CSVHeader['first_name'] = "First Name";
$CSVHeader['last_name'] = "Last Name";
 
$CSV->createTemplate($CSVHeader);
 
$CSVLine = array();
$CSVLine['first_name'] = "Michael";
$CSVLine['last_name'] = "Hartmayer";
 
$CSV->addEntry($CSVLine);
 
file_put_contents("MyCSVFile.csv",$CSV->buildDoc);
Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • LinkedIn
  • Live
  • MySpace
  • Reddit
  • StumbleUpon
Categories: PHP Tags: , ,