PHP: Read TSV and CSV


I wrote the code to handle TSV and CSV.

This method is really similar to RAILS 4 : Load Seed from TSV and CSV

Create TSV and CSV class

First, create classes which represent CSV and TSV, and Factory class.

All right. In this time, I didn’t used yield method. So it can be used with old version PHP.

Well, I must have create FileIterator and create Xsv class which inherits FileIterator.

Attention

The first line of TSV and CSV should contain column names, and data start at second line. And at last line should ends without LF.

Usage

At the following code, you can get data number and its data.

And, $items structure is like the following.

So you can get column data like $item['column_name_1'].