Solved

fgetcsv not working with quotes

Posted on 2008-10-02
8
802 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 

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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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 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…

627 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