Solved

fgetcsv not working with quotes

Posted on 2008-10-02
8
748 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 500 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

919 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now