php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #45356 fgetcsv() ? symbol stripped if first char in cell
Submitted: 2008-06-25 17:45 UTC Modified: 2008-07-27 01:00 UTC
Votes:11
Avg. Score:4.6 ± 0.6
Reproduced:10 of 10 (100.0%)
Same Version:3 (30.0%)
Same OS:9 (90.0%)
From: al at txtlocal dot com Assigned:
Status: No Feedback Package: Filesystem function related
PHP Version: 5.2.6 OS: Linux
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2008-06-25 17:45 UTC] al at txtlocal dot com
Description:
------------
If you have csv file: 

name,price
James,?150

fgetcsv() will remove the ?.

All other chars seem to be fine.

I have searched forums for an answer to this and there are a few people reporting the same - but no definitive answer.




Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-06-25 17:46 UTC] al at txtlocal dot com
In addition - this is only if the ? character in the first char in a "cell".  This would work fine: 

name,price
James,1?50
 [2008-06-25 18:08 UTC] al at txtlocal dot com
If you have csv file: 

name,price
James,?150

fgetcsv() will remove the ?.

All other chars seem to be fine.

I have searched forums for an answer to this and there are a few people
reporting the same - but no definitive answer.


In addition - this is only if the ? character in the first char in a
"cell".  This would work fine: 

name,price
James,1?50
 [2008-07-19 17:50 UTC] mj@php.net
Thank you for this bug report. To properly diagnose the problem, we
need a short but complete example script to be able to reproduce
this bug ourselves. 

A proper reproducing script starts with <?php and ends with ?>,
is max. 10-20 lines long and does not require any external 
resources such as databases, etc. If the script requires a 
database to demonstrate the issue, please make sure it creates 
all necessary tables, stored procedures etc.

Please avoid embedding huge scripts into the report.

I'm unable to reproduce it with a simple scripts neither with 5.2.6 nor with 5.3.0-dev.
 [2008-07-27 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a week, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
 [2008-09-08 19:04 UTC] mk at kurznet dot com
if have the same problem with php 5.2.6
the csv file looks like this: ???123???;auo123???

$handle = fopen($path."Mappe3.csv","r");
   	while ($data = fgetcsv ($handle, 4096, ";")) { 
	
	print_r($data);
	}
fclose ($handle);

Array
(
    [0] => 123???
    [1] => auo123???
)

with PHP 5.2.5 and 4.4.8  everything is ok ? 
is this a bug or a feature ?
 [2008-09-08 22:06 UTC] sfschiller at gmail dot com
based on [mk at kurznet dot com]

a change of the locale information helps.

setlocale(LC_ALL,'de_DE.8859-1');


setting the locale information to a unicode or UTF locale names will lose the first letters.
 [2011-08-19 13:15 UTC] gtisza at gmail dot com
fgetcsv() seems to throw the first character away if it is invalid in the current locale, but ignores invalid characters which are not at the beginning of a cell. This code reproduces the problem in PHP 5.3.6:

<?php

setlocale(LC_ALL,'C');
$utfchar = chr(0xC3).chr(0x89); // U+009C in UTF-8

$csv = $utfchar."x".$utfchar."x\n";

file_put_contents('test.csv', $csv);
$file = fopen('test.csv', 'r');
$data = fgetcsv($file);

for ($i = 0; $i < strlen($data[0]); $i++) {
    echo dechex(ord($data[0][$i])).' ';
}
echo "\n";
unlink('test.csv');

// expected: c3 89 78 c3 89 78 - "ÉxÉx"
// actual: 78 c3 89 78 - "xÉx"

?>

I agree with the commenter in bug 12127 that a CSV function should not mess with encodings in the first place, just copy the content byte-by-byte.
 [2012-06-19 14:36 UTC] info at johanneshoppe dot com
This happens to every NON ASCII char as the first char in a cell.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 22:01:26 2024 UTC