PHP Code

How to parse a CSV file with PHP

It's a very common feature request in any web application to be able to upload data in a CSV file rather than having to manually enter every single item. In that case, you need to build in some intelligence to be able to parse the data from the CSV file.

Well, I love the fact that no programming problem is unique, and many people have done this very thing before you. So, here's someone that's been kind enough to share freely the PHP class they built that can
parse and display data from a CSV file. (Here's the link)

Basically, it opens and parses the lines of a provided CSV file. The data is then stored in an array class variable. The class can also be used to display the CSV data in an HTML table. And you can customize the layout of the display by using a separate PHP template script. Not bad!

Share

4 Comments

  1. Or you could just use the PHP function fgetcsv http://uk2.php.net/fgetcsv

  2. PHP classes website is not really good in my opinion.
    I like PEAR better, they also have a good solid product for csv parsing.


    http://pear.php.net/package/File_CSV_DataSource
    This of course assumes that you are using the PEAR Package System.
    if not, the package only has one file, so you can download it and do a require_once 'xxx';
    and use it!

  3. Your statement of "…no programming problem is unique" – it's so true! I found James Benson's suggestion good, using the php function fgetcsv. Thanks for your posts.

  4. It is a great help for me because I am working on a project and facing problem to parse a csv file. php is a great and you are great.

Leave a Reply

Your email address will not be published. Required fields are marked *

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Anti-spam image