jimfrith
asked on
fgetcsv not working with quotes
I'm using fgetcsv to upload a csv file to mysql. the problem is that some fields have a quote representing inches and its confusing everything and adding the all fields together into one entry.
Some fields are in quotes because the field contains a comma. Does anyone know how to deal with these " so that the fields aren't being joined together?
Some fields are in quotes because the field contains a comma. Does anyone know how to deal with these " so that the fields aren't being joined together?
ASKER
This is the problem though that I do not have control over the csv file.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Well that doesn't work either before adding that I was getting this as my result:
TEA CUP SAUCER 6 1/4",PLAT WAVE"
thats returned exactly like that as one field when it should be two.
with the code you suggested I get this:
TEA CUP SAUCER 6 1/4"",PLAT WAVE""
again as one field. what I want to have returned is this.
TEA CUP SAUCER 6 1/4"
PLAT WAVE
2 fields
TEA CUP SAUCER 6 1/4",PLAT WAVE"
thats returned exactly like that as one field when it should be two.
with the code you suggested I get this:
TEA CUP SAUCER 6 1/4"",PLAT WAVE""
again as one field. what I want to have returned is this.
TEA CUP SAUCER 6 1/4"
PLAT WAVE
2 fields
>>Some fields are in quotes because the field contains a comma.
So some do have the quotes but those that do not have the comma do not have the quotes?
So some do have the quotes but those that do not have the comma do not have the quotes?
ASKER
yes exactly.
the field I seem to be having the problem with does have the quotes though
"BONE OVAL PLATTER 13.75" "
But some fields do not. when I hit a record with a quote in it representing inches It adds all the fields together until it hits another quote.
the field I seem to be having the problem with does have the quotes though
"BONE OVAL PLATTER 13.75" "
But some fields do not. when I hit a record with a quote in it representing inches It adds all the fields together until it hits another quote.
ASKER
The fields without quotes are always a single word. there are only quotes on the field if there are multiple words.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
"Joe","Smith","35" high","metal"
you need to change that to:
"Joe","Smith","35"" high","metal"
So, the bug is in the tool generating the csv file. If you are using a custom script, make sure you change it so that the quote is "escaped" by generating back-to-back double quotes instead.