|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2018-08-11 13:56 UTC] alex at render dot es
Description: ------------ --- From manual page: http://www.php.net/function.fgetcsv --- fgetcsv don't parse correctly JSON in a cell. When php parse the cell with; csv_delimiter = ; enclosure = " and the csv has a cell with JSON text like: {"test":"this is \"test\""} the fgetcsv parse will done: {"test":"this is \"test\"""} this a bad JSON format and the error come when when you try to make a json_decode() with the result. Test script: --------------- //CSV "{""test"":""this is \""test\""""}" <?php ini_set('auto_detect_line_endings',TRUE); $f = fopen('test.csv', "r"); $csv_array=array(); // , $enclosure $i=0; while (($line = fgetcsv($f, 0, "\"")) !== false) { foreach ($line as $cell) { $final_json = json_decode($cell); var_dump($final_json); } } ?> PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2026 The PHP GroupAll rights reserved. |
Last updated: Wed Oct 07 20:00:02 2026 UTC |
I think you have too many '"' in your CSV. "{""test"":""this is \"test\"""}"Thanks for your answers. the number of CSV '"' are correct (the CSV escape with '"'). The problem was with the escape character that need to be '"': fgetcsv($f, 0, ';', '"', '"') work ok. Is confusing because fgetcsv work with the JSON: "{""test"":""this is test""}" without change the default format: fgetcsv($f, 0) and in this case the escape is '"' not the default '\' Why... ??????