Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

fgetcsv not working with quotes

Posted on 2008-10-02
8
Medium Priority
?
849 Views
Last Modified: 2013-12-12
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?

0
Comment
Question by:jimfrith
  • 5
  • 3
8 Comments
 
LVL 82

Expert Comment

by:hielo
ID: 22630488
you will need to replace those double quotes with back-to-back double quotes. So, if you currently have:
"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.
0
 

Author Comment

by:jimfrith
ID: 22630576
This is the problem though that I do not have control over the csv file.
0
 
LVL 82

Assisted Solution

by:hielo
hielo earned 2000 total points
ID: 22638761
try something like:
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
	$data = str_replace('"",""','","',str_replace('"','""',$data));
	echo $data;
}

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:jimfrith
ID: 22651540
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
0
 
LVL 82

Expert Comment

by:hielo
ID: 22651721
>>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?
0
 

Author Comment

by:jimfrith
ID: 22651837
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.  
0
 

Author Comment

by:jimfrith
ID: 22691559
The fields without quotes are always a single word.  there are only quotes on the field if there are multiple words.
0
 

Accepted Solution

by:
jimfrith earned 0 total points
ID: 22692469
I have a solution that works in access but I would like to have it in php so that its automated.  right now if I import the file into a single record in access and run this I can get a proper csv that I can process.

I guess I would have to process the file first save it as another csv and then run fgetcsv to imort it into the db.
the two fields that were giving me trouble were description1 and description2.

here is the access code if anyone can help.





Private Sub cmdFixQuotes_Click()
Dim i As Integer
Dim intStartPos As Integer
Dim intEndPos As Integer
Dim strWrk As String
Dim strLeft As String
Dim strRight As String
Dim strDesc1 As String
Dim strDesc2 As String
 
'Dim strmsg As String
 
With Me.Recordset
.MoveFirst
.MoveNext                   ' skip headings
Do While Not .EOF
 strWrk = ![Field1]
 intStartPos = 1
 For i = 1 To 14                                     ' locate start position of Description1
 intStartPos = InStr(intStartPos, strWrk, ",") + 1
 Next
 intEndPos = InStr(intStartPos, strWrk, ",") - 1
 
 strLeft = Mid(strWrk, 1, intStartPos - 1)
 strDesc1 = Mid(strWrk, intStartPos, (intEndPos - intStartPos) + 1)
 If Mid(strDesc1, 1, 1) = Chr(34) Then               ' remove quotes if there
  strDesc1 = Mid(strDesc1, 2, Len(strDesc1) - 2)
 End If
 
 intStartPos = intEndPos + 2                          ' set start position of Description2
 intEndPos = InStr(intStartPos, strWrk, ",") - 1      ' set end position of Description2
 strRight = Mid(strWrk, intEndPos + 1, Len(strWrk) - intEndPos)
 strDesc2 = Mid(strWrk, intStartPos, (intEndPos - intStartPos) + 1)
 If Mid(strDesc2, 1, 1) = Chr(34) Then                ' remove quotes if there
  strDesc2 = Mid(strDesc2, 2, Len(strDesc2) - 2)
 End If
 
 
 strDesc1 = Chr(34) & Replace(strDesc1, Chr(34), Chr(34) & Chr(34)) & Chr(34)
 strDesc2 = Chr(34) & Replace(strDesc2, Chr(34), Chr(34) & Chr(34)) & Chr(34)
 
 .Edit
 !Field1 = strLeft & strDesc1 & "," & strDesc2 & strRight
 .Update
 
  .MoveNext
Loop
End With
End Su

Open in new window

0

Featured Post

[Webinar] Cloud Security

In this webinar you will learn:

-Why existing firewall and DMZ architectures are not suited for securing cloud applications
-How to make your enterprise “Cloud Ready”, and fix your aging DMZ architecture
-How to transform your enterprise and become a Cloud Enabler

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question