Skip to content

Commit

Permalink
Merge pull request #3 from misseur/fix/24943
Browse files Browse the repository at this point in the history
[FIX#24943] Ajout d'un str_remplace pour éviter des bugs liés aux "\n"
  • Loading branch information
Guillaume Dubost committed Jan 26, 2016
2 parents 9e4f1ea + 00945ed commit dbc916a
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 1 deletion.
6 changes: 6 additions & 0 deletions Tests/Functional/features/csv/csv.feature
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ Scénario: Transformer un simple tableau
Et le résultat devrait être identique au fichier csv "csv_from_simple_array.csv"
Et le csv sortant devrait contenir 1 lignes

Scénario: Transformer un simple tableau avec des "\"
Quand je convertis en csv le tableau contenu dans "simple_array_with_backslash_n.json"
Alors il ne devrait pas y avoir eu une erreur
Et le résultat devrait être identique au fichier csv "csv_from_simple_array_with_backslash_n.csv"
Et le csv sortant devrait contenir 1 lignes

Scénario: Transformer un tableau contenant plusieurs lignes
Quand je convertis en csv le tableau contenu dans "multilines_array.json"
Alors il ne devrait pas y avoir eu une erreur
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[
{
"champ_1": "val\n_1",
"champ_2": "val_2\n",
"champ_3": "\nval_3",
"champ_4": "va\nl_4",
"champ_5": "val_5\n",
"champ_6": "\nv\na\nl\n_\n6\n"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
champ_1;champ_2;champ_3;champ_4;champ_5;champ_6
"val _1";"val_2 ";" val_3";"va l_4";"val_5 ";" v a l _ 6 "
4 changes: 3 additions & 1 deletion src/CsvUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ public static function arrayToCsv(array $array, int &$csv_rows = null)
if (!empty(array_diff(array_keys($value), $headers))) {
throw new \Exception("Bad csv", 400);
}
$csv .= self::sputcsv(array_values($value), ';', '"', "\n");

$clean_array = str_replace("\n", " ", array_values($value));
$csv .= self::sputcsv($clean_array, ';', '"', "\n");
}
$csv = substr_replace($csv, "", -1);
$csv_rows = count($tokens);
Expand Down

0 comments on commit dbc916a

Please sign in to comment.