php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #76730 fgetcsv don't parse correctly JSON in a cell
Submitted: 2018-08-11 13:56 UTC Modified: 2018-08-11 18:20 UTC
From: alex at render dot es Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 7.1.20 OS: centos 7, osx 10.13.6, debian 8
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: alex at render dot es
New email:
PHP Version: OS:

 

 [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);
		}
	}
?>


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-08-11 14:20 UTC] alex at render dot es
-Operating System: centos 7 +Operating System: centos 7, osx 10.13.6, debian 8
 [2018-08-11 14:20 UTC] alex at render dot es
add more oses
 [2018-08-11 14:59 UTC] alex at render dot es
-Status: Open +Status: Closed
 [2018-08-11 14:59 UTC] alex at render dot es
Is possible do the escape with the '"' and the error disappear
 [2018-08-11 15:05 UTC] a at b dot c dot de
Are your parameters to fgetcsv correct? You're specifying " as the delimiter as the field delimiter but you're using it as the enclosure character. Also, fgetcsv() defaults to using \ as the escape character.
 [2018-08-11 15:08 UTC] a at b dot c dot de
I think you have too many '"' in your CSV.

"{""test"":""this is \"test\"""}"
 [2018-08-11 15:42 UTC] alex at render dot es
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... ??????
 [2018-08-11 15:50 UTC] spam2 at rhsoft dot net
did you try to create a csv with fputcsv and look at the differences between that and you and crafted input? anyways, one would normale use base64 encoding to avoid such issues from start
 [2018-08-11 18:20 UTC] ab@php.net
-Status: Closed +Status: Not a bug
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 18 14:01:31 2024 UTC