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?
I think you need to define a text qualifier, this is called enclosure in fgetcsv.
eg:
This is to tell the parser that it shouldn’t take the comma inside the text area.
You should be able to do:
UPDATE:
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?