CSV & Standard PHP Library

Introduction

I’ve been spending quite some time working with the Zend framework, I must say it’s about the only PHP framework I’ve got on with. I really like the directory structure lay out. With that kind of directory layout and more rigid class names I’m not really sold on the whole idea that we need namespaces from php6.

Standard PHP Library

Any way the SPL (Standard PHP Library) is another little gem that is hidden away in PHP 5. Due the PHP manual not being all that great with class structured documentation they’ve provided some where else for the SPL documentation.

SPL has been around for years now, but it’s still one of those hidden away features that we don’t all know about. I briefly discovered it some years ago, but have only recently started using.

A full introduction to the SPL can be found at sitepoint from article written by Harry Fuecks, who does some other very good PHP related articles, well worth the read.

CSV Classes

There are probably many other PHP CSV classes out there. But I think the ones I provide here are the simplest and easiest I’ve seen. The are a few classes in the package but the ones you’ll be interested in are.

While creating the classes I have taken speed an memory into account so don’t worry it doesn’t do anything crazy like load the entire file into memory first. These CSV classes have been tested with CSV files of over 100MB.

Requirements

The classes are designed to work on there own or with the Zend framework.

The classes have been tested to work with PHP 5.2.3 on Linux and Windows.

The jDempster_Csv_String requires at least PHP 5.1.
The jDempster_Csv_Zip_* classes require PHP 5.2.

Note:
For the classes to read lines correctly auto_detect_line_endings needs to be on. A simple ini_set can turn this on at runtime.

Usage

The following are some sample usage. The classes are commented in a docblock style for extra help and information.

Example 1.

1
2
3
4
5
6
$filename = 'csv_file.csv';
$csv = new jDempster_Csv_File($filename, true);
foreach ($csv as $row) {
    var_dump($row);
}
echo 'Row count: '.count($csv).'';

Example 2.

1
2
3
4
5
6
7
8
$filename = 'csv_file.csv';
$csv = new jDempster_Csv_File($filename, true);
foreach ($csv as $rowNumber => $row) {
    echo $rowNumber.'';
    var_dump($row);
    echo '';
}
echo 'Row count: '.count($csv).'';

Example 3.

1
2
3
4
5
6
7
8
$csvData = file_get_contents($filename);
$csv = new jDempster_Csv_String($csvData, true);
foreach ($csv as $rowNumber => $row) {
    echo $rowNumber.'';
    var_dump($row);
    echo '';
}
echo 'Row count: '.count($csv).'';

Example 4.

1
2
3
4
5
6
7
8
$csvZip = 'csv_zipfile.zip';
$csv = new jDempster_Csv_Zip_File($csvZip, true);
foreach ($csv as $rowNumber => $row) {
    echo $rowNumber.'';
    var_dump($row);
    echo '';
}
echo 'Row count: '.count($csv).'';

As you can see, the object is used as you would any array. It can be used in while loops, for loops etc etc.

Any problems or comments please :)

Downloads

jdempster_csv.zip (6.50 KB)