Reputation: 115
I have a csv file that has 6 lines like the following :
10264,dan,Dan,37519,MILLER,6/3/2014 14:51,$28.40,Rapid,[email protected],,
10443,Dari,"D. R, LLC",37308,SELLERS,6/2/2014 15:12,$40.00,esponse,[email protected],,
10488,EmHern,"A HOME FOR YOU,LLC",37749,MILLER,6/1/2014 8:23,$33.00, Response,[email protected],,
I trying to read that csv and extract some columns then add a pipe delimiter to each column extracted, here is the function I used :
<?php
function fncReadCSV($csvFileName){
$result = array();
$row = 1;
if (($handle = fopen($csvFileName, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$outPut = "";
$outPut .= $data[0];
$outPut .="|";
$outPut .= $data[1];
$outPut .="|";
$outPut .= $data[2];
$outPut .="|";
$outPut .= $data[6];
$outPut .="|";
$outPut .= $data[8];
$outPut .="|";
array_push($result, rtrim($outPut, "|"));
$row++;
}
}
fclose($handle);
return $result;
}
echo "<pre>";
print_r(fncReadCSV("file.csv"));
echo "</pre>";
the result I get is correct for only the first row, for the 2nd, 3rd rows I get the full row as it is in the csv, I'm expecting the quotes to break my code how can I fix that with a code that works for csv files with quotes present and csv files without please?
the result is :
Array
(
[0] => 10264|dan|Dan|$28.40|[email protected]
[1] => 10443,Dari,"D. R, LLC",37308,SELLERS,6/2/2014 15:12,$40.00,esponse,[email protected],,
[2] => 10488,EmHern,"A HOME FOR YOU,LLC",37749,MILLER,6/1/2014 8:23,$33.00, Response,[email protected],,
)
the result I'm expecting is :
Array
(
[0] => 10264|dan|Dan|$28.40|[email protected]
[1] => 10443|Dari|"D. R, LLC"|esponse|[email protected]
[2] => 10488|EmHern|"A HOME FOR YOU,LLC"|37749|[email protected]
)
Upvotes: 1
Views: 6392
Reputation: 21661
fgetcsv() should handle single quotes, just fine in fact the default output of fputcsv() is to quote ( wrap ) any line with spaces in it. having " " around strings with the delimiter is perfectly expected, "words, other words", that is 100% fine with fgetscsv.
try this
echo '<pre>';
if (($handle = fopen($csvFileName, "r")) !== FALSE) {
while (false !== ($data = fgetcsv($handle, 1024))) {
print_r($data);
$data_row = array(
$data[0],
$data[1],
$data[2],
$data[6],
$data[8]
);
$result[] = implode("|", $data_row);
$row++;
}
}
fclose($handle);
return $result;
implode is a bit cleaner and you wont need to trim the extra "|" ?, as i see no purpose for it anyway why not just omit the last ( rightmost ) |.
$outPut .="|";
array_push($result, rtrim($outPut, "|"));
array_push($result, $outPut));
in fact runing your exact code gives me this output?
$result = array();
$row = 1;
if (($handle = fopen(__DIR__.'/test.csv', "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$outPut = "";
$outPut .= $data[0];
$outPut .="|";
$outPut .= $data[1];
$outPut .="|";
$outPut .= $data[2];
$outPut .="|";
$outPut .= $data[6];
$outPut .="|";
$outPut .= $data[8];
$outPut .="|";
array_push($result, rtrim($outPut, "|"));
$row++;
}
}
fclose($handle);
array(
'0' => '10264|dan|Dan|$28.40|[email protected]',
'1' => '10443|Dari|D. R, LLC|$40.00|[email protected]',
'2' => '10488|EmHern|A HOME FOR YOU,LLC|$33.00|[email protected]'
)
running my code gives me
array(
'0' => '10264|dan|Dan|$28.40|[email protected]',
'1' => '10443|Dari|D. R, LLC|$40.00|[email protected]',
'2' => '10488|EmHern|A HOME FOR YOU,LLC|$33.00|[email protected]'
)
here be your solution
echo "<pre>";
$result = array();
$row = 1;
if (($handle = fopen(__DIR__.'/test.csv', "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
if(count($data) == 1){
$f = fopen('php://temp', 'w');
fwrite($f, $data[0]);
rewind($f);
$data = fgetcsv($f, 1000, ",");
fclose($f);
}
$data_row = array(
$data[0],
$data[1],
$data[2],
$data[6],
$data[8]
);
$result[] = implode("|", $data_row);
$row++;
}
}
var_export($result);
fclose($handle);
outputs
array(
'0' => '10264|dan|Dan|$28.40|[email protected]',
'1' => '10443|Dari|D. R, LLC|$40.00|[email protected]',
'2' => '10488|EmHern|A HOME FOR YOU,LLC|$33.00|[email protected]'
)
explination
getcsv returns a single element because of the outer quotes ", but it strips out one set of quotes around the "D. R, LLC" bit ( witch gives us a string similar to line 1), then check for that single element, and write it to the temp buffer, ( stream wrapper ) and reprocess the stream with fgetcsv and voila fixed!
Upvotes: 1