I have the following row in my CSV file

,1,193,23 Video,,"Slotsgade 2, 2.th",,,,,,Copenhagen N,,DK-2200,Denmark,,,,,,,,
,,,Europe,+45 31450980,,info@23company.com,www.23video.com,,,,,,,,Cabsat Online
 Video Industry Sourcebook 12,Mr.,Kristoffer,,Schou,,M,,,,,,,,,,,,,,,,,,,,,,,,,
,,,schou@23company.com,,,,,,,

As you can see

"Slotsgade 2, 2.th"

has a comma in between, now I am using preg_split to explode the row in columns.

Could anyone suggest me a regular expression which skips the comma in between double quotes

I have tried this already:

/,|".*(?=")/      

What is the regular expression that I can use in preg_split to get the data correctly?

有帮助吗?

解决方案

Use the fgetcsv function, it will deal it for you.

And you also have str_getcsv, which will parse a CSV string into an array.

其他提示

From the library of RegexBuddy:

CSV: Any field

Matches any single field in a CSV file. Use this regex to iterate over all fields.

"[^"\r\n]*"|[^,\r\n]*

CSV: Any field with extra whitespace

Matches any single field in a CSV file. Use this regex to iterate over all fields. Includes extra whitespace before and after double-quoted fields in the regex match.

[ \t]*+"[^"\r\n]*+"[ \t]*+|[^,\r\n]*+

CSV: Complete row, all fields

Match complete rows in a comma-delimited file that has 3 fields per row, capturing each field into a backreference. To match CSV rows with more or fewer fields, simply duplicate or delete the capturing groups.

^("[^"\r\n]*"|[^,\r\n]*),("[^"\r\n]*"|[^,\r\n]*),("[^"\r\n]*"|[^,\r\n]*)$
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top