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)

Xdebug __get memory leak

Just spent a few hours trying to work out why some php scripts ended up using more than 300MB of memory when the php.ini memory limit was set to 50MB.

I thought it was a leak in php, after alot of testing I found that in actual fact Xdebug 2.0.0RC4 was causing the leak.

1
2
3
4
5
6
7
8
9
10
class test1 {
    public function __get($name) {
        return;
    }
}
 
$test = new test1();
while(true) {
    $test->test;
}

jQuery Disable Text Select Plugin

Introduction

While making html elements dragable and using them as clickable areas I noticed some little annoyances, mainly the area being selected. This occurs if the user happends to double click, or if the user clicks some element to expand an item then quickly clicking it again to collapse it. The text would become selected. I personally found this very annoying.

So I’ve written a pretty basic jQuery plugin to help with the problem.

Requirements

Compatibility

So far the plugin has been tested in windows using the following browsers.

Usage

HTML

1
2
<p class="disableSelection">
    this text can't be selected.

Javascript

1
2
3
$(function() {
    $('.disableSelection').disableTextSelect();
});

Downloads

jquery-disable.text.select.example.htm
jquery.disable.text.select.js (1.16 KB)
jquery.disable.text.select.pack.js (1.07 KB)

← Previous Page