Answer the question
In order to leave comments, you need to log in
How to parse a crooked csv file?
Good day.
There is a task, one of which is the output of data from a csv file to a table.
But the problem is that the separator is " , " - a comma, and the same comma occurs inside the cell.
Because of this, one cell is split into 2, and this should not be so.
Download csv
How to solve the problem?
My code:
<?php
$path = '/upload/file_csv.csv';
$csv = file_get_contents($path, "r");
$arCsv = explode("\n", $csv);
foreach ($arCsv as $key => $value){
$arCsvFinal[] = explode (",", $value);
}
echo '<table border="1">';
foreach ($arCsvFinal as $key => $value){
echo "<tr>";
foreach ($value as $key2 => $value2){
echo "<td>";
echo $value2;
echo "</td>";
}
echo "</tr>";
}
echo "</table>";
//var_dump($arCsvFinal);
?>
$path = '/upload/file_csv.csv';
$csv = fopen($path, "r");
$arCsv = fgetcsv ( $csv, $delimiter = ",");
var_dump($arCsv);
Answer the question
In order to leave comments, you need to log in
The CSV file is not crooked, GOOGLE opens it just fine.
In the "raw" form in the CSV file, the comma in the text appears inside double quotes. If you really want to parse yourself, then consider double quotes, and not escaped by a backslash. If not, then use the already written functionality php.net/manual/en/function.fgetcsv.php.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question