deGee
deGee

Reputation: 801

parse csv (data encased withing quotes) using fgetcsv() not working properly

I am trying to parse a csv file where my data is given in a format like :

"Total","Shazam (DE)","Total",,"215,611","538","£935.97","£60.77"

Now if I give fgetcsv() like the one below :

$values = fgetcsv($f,8098);

It returns me an array as :

array(9) {
  [0]=>
    string(15) ""Total""
  [1]=>
    string(27) ""Shazam (DE)""
  [2]=>
    string(15) ""Total""
  [3]=>
    string(1) ""
  [4]=>
    string(9) ""215"
  [5]=>
    string(9) "611""
  [6]=>
    string(11) ""538""
  [7]=>
    string(19) ""£935.97""
  [8]=>
    string(19) ""£60.77"
 "
 }

but now , If I give a if statement like :

 $values = fgetcsv($f,8098);
 if ($values[0] == "Total")
   echo "Hello";

it's not echoing out anything , why the condition isn't satisfied ? Is it something to do with the enclosing quotes ?

I even tried escaping quotes e.g .

 if ($values[0] == '\"Total\"')
   echo "Hello";

but to no use.

Do , I need to change any .ini settings for this ? Please help me.

EDIT : I tried doing var_dump , and it did show my values containg quotes :

 array(9) {
  [0]=>
   string(15) ""Total""
 [1]=>
   string(27) ""Shazam (DE)""
 [2]=>
   string(15) ""Total""
 [3]=>
   string(1) ""
 ...
}

but even

$values[0] == "\"Total\""

didn't work . What should be the right comparator ?

UPDATE : After few hours of aimless trial and error , finally found something annoying , but it fixed my problem .

Initially , when I tried to open my csv file , it opened a notepad with ÿþD"" as it's content . What , I did is , copied the entire filecontent and created a new csv file with the same name , and to my surprise , it worked .

The old file was opening nicely in MS-EXCEL / NOTEPAD , but in excel , each line was being displayed inside one single cell .

My question is , what was the issue with the .csv fromat ? Has anyone coome across with such problem before?

Upvotes: 3

Views: 5136

Answers (1)

Jim Wolff
Jim Wolff

Reputation: 5210

I think you need to define a text qualifier, this is called enclosure in fgetcsv. eg:

$values = fgetcsv($f,8098,'"'); 
// the last part is: singlequote doublequote singlequote,
// you could also use an escape "\"" 

This is to tell the parser that it shouldn't take the comma inside the text area.

You should be able to do:

$values[0] == '"Total"'

UPDATE:

You problem with the file opening oddly, is probably related to the files encoding, by creating a new file, you created it with a new encoding maybe ANSI/UTF-8/Unicode something the original file wasnt, and then copied the content in there, so now it was saved in that format instead.. im not sure why this would give you the problems you are experiencing, but im assume the parser somehow cant handle the wrong encoding.

try opening notepad and notice when you click save as, you can choose encoding in the buttom, try with a few different files with different encoding if you want to make sure that was it?

Upvotes: 2

Related Questions