views:

54

answers:

1

Using PHP, reading in a string like the following: 36,2,"$21,830.00","$18,012.50","$20,764.00","$14,935.00","$13,655.00","$15,820.00","$6,895.00","$4,357.50","$4,944.00"

I want to explode using the comma (,) as a delimiter, but I've realized there's a problem- my money values also have commas! Can anyone suggest a way to handle this? I want to explode based on commas, but not if I'm inside quotes.

Thanks!

+2  A: 

Try using str_getcsv function. It will allow you to specify the enclosing character that should solve your problem.

Edit: didn't realize this is only for PHP 5.3+ so might not be an option for you (although the comments have some possible workarounds)

Marek Karbarz
Damn, that looks perfect, but I'm running 5.2 and don't think I can upgrade.
Airjoe
There's always fgetcsv in earlier versions, though it's a bit sad to have to write and read a file just to get CSV parsing. Definitely a good idea to parse CSV with a proper CSV parser, though, instead of some wobbly regex hack.
bobince
The php page that Marek K linked to had a user-written function in the comment that works for me. Thanks so much for the help!
Airjoe