#38471 [NEW]: fgetcsv(): locale dependency of delimiter / enclosure arg
| From: | jo at feuersee dot de | Date: | Wed, 16 Aug 2006 12:15:04 +0000 |
| Subject: | #38471 [NEW]: fgetcsv(): locale dependency of delimiter / enclosure arg | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-100853@lists.php.net to get a copy of this message | ||
From: jo at feuersee dot de
Operating system: Linux
PHP version: 5.1.4
PHP Bug Type: Filesystem function related
Bug description: fgetcsv(): locale dependency of delimiter / enclosure arg
Description:
------------
fgetcsv() is affected by locale settings in a way the PHP
documentation dosen't imply. It says:
"Locale setting is taken into account by this function. If
LANG is e.g. en_US.UTF-8, files in one-byte encoding are
read wrong by this function."
In the example below, reading CSV data is broken because
language settings affect the character following the
delimiter (even when delimiter is a 1 byte character).
I expected that fgetcsv() would work independent of locale
settings as long as delimiter and enclosing are US-ASCII
clean.
To my surprise, using multibyte UTF-8 characters (and
proper locale setting) as delimiter didn't throw a
warning, but didn't work as expected. It seems that only
the 1st byte (the documentation says character) is used?
Other side effects:
The documentation isn't clear about the enclosure arg:
- The enclosure arg is optional but defaults to a "
- Explicit setting an empty enclosure '' throws a warning,
suggesting that the CSV data requires enclosing
- But fgetcsv() (with or w/o enclosure) works on both
enclosed and non-enclosed CSV data, seems it's optional
anyway?
- It is unclear which setlocale() category arg affects
fgetcsv() behavior. By testing, LC_ALL and LC_CTYPE did
work, I did expect LC_COLLATE (instead of LC_CTYPE)
Reproduce code:
---------------
<?php
$f = fopen('test.csv', 'r');
if(is_resource($f)) {
while(($data = fgetcsv($f, 1000, ';')) !== FALSE) {
print_r($data);
}
fclose($f);
}
?>
And give a test.csv in UTF-8 encoding with multibyte characters in the 1st
place after delimiter:
A;Äbc
B;bcd
--eof
And let the PHP process either inherit LANG=C or set it explicit or add
setlocale(LC_CTYPE, 'C');
above
Expected result:
----------------
Array
(
[0] => A
[1] => Äbc
)
Array
(
[0] => B
[1] => bcd
)
Actual result:
--------------
Array
(
[0] => A
[1] => bc
)
Array
(
[0] => B
[1] => bcd
)
--
Edit bug report at http://bugs.php.net/?id=38471&edit=1
--
Try a CVS snapshot (PHP 4.4): http://bugs.php.net/fix.php?id=38471&r=trysnapshot44
Try a CVS snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=38471&r=trysnapshot52
Try a CVS snapshot (PHP 6.0): http://bugs.php.net/fix.php?id=38471&r=trysnapshot60
Fixed in CVS: http://bugs.php.net/fix.php?id=38471&r=fixedcvs
Fixed in release: http://bugs.php.net/fix.php?id=38471&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=38471&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=38471&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=38471&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=38471&r=support
Expected behavior: http://bugs.php.net/fix.php?id=38471&r=notwrong
Not enough info: http://bugs.php.net/fix.php?id=38471&r=notenoughinfo
Submitted twice: http://bugs.php.net/fix.php?id=38471&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=38471&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=38471&r=php3
Daylight Savings: http://bugs.php.net/fix.php?id=38471&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=38471&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=38471&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=38471&r=float
No Zend Extensions: http://bugs.php.net/fix.php?id=38471&r=nozend
MySQL Configuration Error: http://bugs.php.net/fix.php?id=38471&r=mysqlcfg