|Delimiter in one string||#1|
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.
posted date: 2009-04-14 13:05:00
|Re: Delimiter in one string||#3|
Your data string doesn't self-evidently match the header - you have 5 columns worth of data. (You also have serious spelling and capitalization issues with "were are" and "california".) The strings presented consist of 5 fields, not 4.
posted date: 2009-04-14 13:09:00
|Re: Delimiter in one string||#4|
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
posted date: 2009-04-14 13:11:00
|Re: Delimiter in one string||#5|
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.
posted date: 2009-04-14 13:13:00
|Re: Delimiter in one string||#6|
+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.
posted date: 2009-04-14 13:29:00
|Re: Delimiter in one string||#7|
No need to reinvent any wheels, PHP already has what you need in fgetcsv
posted date: 2009-04-14 13:34:00
|select page: « 1 »|