Solved

fgetcsv not working with quotes

Posted on 2008-10-02
8
741 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

762 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

19 Experts available now in Live!

Get 1:1 Help Now