php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #44979 Strange behavior of fgetcsv
Submitted: 2008-05-13 10:32 UTC Modified: 2008-05-14 08:51 UTC
From: great_boba at yahoo dot com Assigned:
Status: Not a bug Package: Filesystem function related
PHP Version: 5.2.6 OS: Suse
Private report: No CVE-ID: None
 [2008-05-13 10:32 UTC] great_boba at yahoo dot com
Description:
------------
I try to write some application which parse csv-dump. 
Below I write little example to show strange things.

Reproduce code:
---------------
1.csv
"A","B","C"
"A","B "","C """
1.php
<?php

$fp=fopen('1.csv',r');
while($data=fgetcsv($fp,4096,','))
   print_r($data);

fclose($fp);

?>

Expected result:
----------------
Array
{
  [0]=>A
  [1]=>B
  [2]=>C
}
Array
{
  [0]=>A
  [1]=>B "
  [2]=>C ""
}

Actual result:
--------------
Array
{
  [0]=>A
  [1]=>B
  [2]=>C
}
Array
{
  [0]=>A
  [1]=>B ",C """
}

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-05-13 13:02 UTC] felipe@php.net
This is expected. The "" is an escaped enclosure.

Using:
"A","B","C"
"A","B ""","C """

Array
(
    [0] => A
    [1] => B "
    [2] => C "
)

Use a proper format file.
 [2008-05-14 08:51 UTC] great_boba at yahoo dot com
Hello Felipe!

I have kmail address book. I convert it in csv-format where I have some field containing data like this Vladimir "Great" Struchkov. Kmail converter save this data in csv-file as "Vladimir "Great" Struchkov"

In my dump all fields are in quotes. But if some fields (not all) are in quotes I think that we can have some rules to parse this file.
1) If field is in quotes we must have comma after last qoute
2) etc
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Jul 08 20:01:30 2024 UTC