views:

171

answers:

3

Hello,

Here is an example of a file that i use to create a mysql db. The delimiter is "," but in the description for a single column exist ",".

Header: City State Zip Description
Los Angeles, California , 98005, "welcome to california, were are living the dream","Please stay, a while."

The problem is that the description in "quotes" contains a delimiter which causes the file to have additional columns.

Someone told me that regex or preg_match functions my be able to solve my problem. Can someone tell me how.

+1  A: 

This is a pretty well solved problem, which is the standard for the CSV (or Comma Seperated Values) format. The easiest way to approach parsing CSV is to use a CSV library which has already been tested and works reliably. A CSV parser exists for just about every programming language. There are a handful of special cases you haven't thought of, so it's worth it to use an existing library in most cases.

This page is a good resource for CSV parsing:

http://www.creativyst.com/Doc/Articles/CSV/CSV01.htm

danieltalsky
+1 real parsers tend to be most reliable. Although, beware the PHP built-in str_getcsv etc. function which use an unusual quote-escaping dialect (\\") that may or may not match your input.
bobince
A: 

For each field, you need a regex like:

("(?:[^"]+|"")*"|[^,]+)

That is two alternatives. The first matches a double quote followed by a pattern of zero or more repeats of another alternative, which is either a string of non-double-quotes or a pair of double quotes (to allow double quotes to appear in your string if doubled up). The second alternative matches a field without double quotes, matching a string of non-commas. You'd then combine these with comma matches.

Jonathan Leffler
Johnathan,Thanks for your reply, I will try this.Thanks to Daniel too for the link-great resource
+7  A: 

No need to reinvent any wheels, PHP already has what you need in fgetcsv

Ole J. Helgesen