Page 1 of 1

parsing CSV files question

Posted: Fri Jul 03, 2009 4:08 pm
by korvirlol
Hello,

So i am working on a project for a company where i am taking a (large) CSV file exported from another program, and parsing this data into nice, easy to read graphical data - relatively simple idea.

Here is the problem:
The data in the CSV file contains dollar amounts, and dollar amounts of greater then $999 is in the format: 1,234.00. You might be able to see the problem.

So when i explode the data in the file on commas i get the following (for example):
[1026] => "11
[1027] => 541.00"

the original amount stored in the file was: "11,541.00"

so the explode is splitting this into 2 parts.. but i need just the 1 value.

I tries using preg_replace to only replace commas in between quotes with nothing, but this was just creating a huge mess.

If anyone has any suggestions about how i could better parse this, PLEASE enlighten me!

Thanks,

kris

Re: parsing CSV files question

Posted: Fri Jul 03, 2009 4:12 pm
by Christopher
Have you looked a PHP's CSV import functions? They should handle quoted values with ease. I have a class that wraps them for reading delimited files (because Luke wouldn't fix his! ;))

Re: parsing CSV files question

Posted: Fri Jul 03, 2009 4:14 pm
by korvirlol
Yea,

i tried using str_getcsv()

but when i try this:
Fatal error: Call to undefined function str_getcsv()

at first i thought it might be an extension not enabled in php... but i could not find reference to an extension that supports this.

Re: parsing CSV files question

Posted: Fri Jul 03, 2009 4:20 pm
by korvirlol
ahh i found fgetcsv() ...

problem solved, disregard this!