#38471 [Opn->WFx]: fgetcsv(): locale dependency of delimiter / enclosure arg

From: Date: Wed, 16 Aug 2006 12:44:50 +0000
Subject: #38471 [Opn->WFx]: fgetcsv(): locale dependency of delimiter / enclosure arg
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-100855@lists.php.net to get a copy of this message
 ID:               38471
 Updated by:       tony2001@php.net
 Reported By:      jo at feuersee dot de
-Status:           Open
+Status:           Wont fix
 Bug Type:         Filesystem function related
 Operating System: Linux
 PHP Version:      5.1.4
 New Comment:

We're working Unicode support in PHP6. but it won't appear in previous
versions.


Previous Comments:
------------------------------------------------------------------------

[2006-08-16 12:15:03] jo at feuersee dot de

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 this bug report at http://bugs.php.net/?id=38471&edit=1


Thread (2 messages)

« previous php.bugs (#100855) next »