Ruben Ruben - 3 months ago 7
PHP Question

CSV string change with php

Hi I would like to eliminate this sign " (quotation mark) in CSV file with PHP. I'm using this file with amchart, but with quotatnion mark chart doesn't work.

File is :
"date1,value1,value2,value3,value4,value5,valu6,value7,value8,value9"

"date2,value1,value2,value3,value4,value5,valu6,value7,value8,value9"

"date3,value1,value2,value3,value4,value5,valu6,value7,value8,value9"

"date4,value1,value2,value3,value4,value5,valu6,value7,value8,value9"

"date5,value1,value2,value3,value4,value5,valu6,value7,value8,value9"

I'm using this code:

error_reporting(0);
$records = array_map('str_getcsv', file('http://localhost/projekt/file.csv'));
$data =array();
foreach($records as $row){
if($row[0] != 'date'){
$data[] = array(
'date' => $row[0],
'open'=> $row[1],
'high'=> $row[2],
'low'=> $row[3],
'close'=> $row[4],
'column-5'=> $row[5],
'column-6'=> $row[6],
'column-7'=> $row[7],
'column-8'=> $row[8],
'column-9'=> $row[9],
'column-10'=> $row[10],
'column-11'=> $row[11],
'column-12'=> $row[12],
'column-13'=> $row[13],
);
}
}
$chartdata = json_encode($data);

$chartdata = str_replace('\/','/',$chartdata);

Answer

Why not just do a smaller change

 $records = array_map('str_getcsv',
    str_replace('"','',file('http://localhost/projekt/file.csv')));