To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
PHPBuilder.com  
 

 

Go Back   PHPBuilder.com > PHP Help > Coding

Coding Help with PHP coding

Reply
 
Thread Tools Rate Thread Display Modes
Old 11-23-2009, 12:16 AM   #1
gjewett
Junior Member
 
Join Date: Dec 2006
Posts: 9
Escaping quotes when reading a CSV file?

I am importing a CSV file that is generated by a Point of Sale application. Unfortunately the CSV they output does not have properly escaped quotes, so a line often looks like this:

1010, "Item Description of 14" tall tree", "Blue", $10.00, 3

The extra quote in the item description is causing problems when reading this data with PHP. There are thousands of items in the database so going through and manually removing the quotes from each item description is not a great option.

Is there a way to use regular expressions or something like that to go through the whole CSV file and escape all of the extra quotes?

Thanks!
gjewett is offline   Reply With Quote
Old 11-23-2009, 01:25 AM   #2
NogDog
High Energy Magic Dept.
 
NogDog's Avatar
 
Join Date: Aug 2006
Location: Ankh-Morpork
Posts: 11,999
Maybe....
PHP Code:
$csv = preg_replace('/(?<!,)"(?!,)/', '""', $csv);
The idea here is to double up any double quote that is not immediately preceded or followed by a comma. This may need to be altered if your example is accurate and there is a space after each comma.
__________________
"That's what the gods are! An answer that will do! Because there's food to be caught and babies to be born and life to be lived and so there is not time for big, complicated, and worrying answers! Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be." -- from Nation, by Terry Pratchett
freelancer.internet.com
Email me
NogDog is offline   Reply With Quote
Old 11-23-2009, 01:51 PM   #3
gjewett
Junior Member
 
Join Date: Dec 2006
Posts: 9
Thanks!

Works flawlessly (you were right, my example was wrong, no spaces after commas). Thanks very much for your help. Time for me to spend a day learning to use regular expressions!
gjewett is offline   Reply With Quote
Old 11-23-2009, 04:05 PM   #4
NogDog
High Energy Magic Dept.
 
NogDog's Avatar
 
Join Date: Aug 2006
Location: Ankh-Morpork
Posts: 11,999
Quote:
Originally Posted by gjewett View Post
Works flawlessly (you were right, my example was wrong, no spaces after commas). Thanks very much for your help. Time for me to spend a day learning to use regular expressions!
If you master it in a day, I'll be impressed.
__________________
"That's what the gods are! An answer that will do! Because there's food to be caught and babies to be born and life to be lived and so there is not time for big, complicated, and worrying answers! Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be." -- from Nation, by Terry Pratchett
freelancer.internet.com
Email me
NogDog is offline   Reply With Quote
Reply

Bookmarks

Tags
csv, escape quotes, reading csv


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT -4. The time now is 01:29 PM.






Acceptable Use Policy


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.