一个字符串中的分隔符

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.

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

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.

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