Page 1 of 1

fgetcsv() function not recognising GBP - british pound chara

Posted: Wed Jun 18, 2008 6:18 am
by samadams83
I am trying to get data from a CSV file using the fgetscv function, but it seems to be ignoring the british pound character.
E.g. when i try and collect the data £500 from the file, it is returned as just 500.
I am attempting to use the function like this:

Code: Select all

while ($row = fgetcsv($file_handle, 2000, $delimiter))
{
    //do something with the data
}
I also know the data is correct in the file itself, because this much clumsier version returns the right value:

Code: Select all

 
while ($line = fgets($file_handle))
{
    $row = explode($delimiter, $line);
}
 
I would use this version, but it doesn't have the qualifier (e.g. double-quote) functionality i need.

Without using a large parsing function to get through the data, does anyone know how i can make fgetcsv() not strip out the £(pound) sign?

Cheers

Re: fgetcsv() function not recognising GBP - british pound chara

Posted: Wed Jun 25, 2008 12:28 pm
by Txtlocal
Hi Sam,

Did you ever find a result for this? Its been bugging me for hours... all characters seem to work fine with fgetcsv(), except £ which is stripped. :(

Please help!

cheers.

Re: fgetcsv() function not recognising GBP - british pound chara

Posted: Wed Jun 25, 2008 12:32 pm
by Txtlocal
If the £ character is not the first character in the "cell" then the £ is imported fine..... it is only when it is the first character it is ignored...

Re: fgetcsv() function not recognising GBP - british pound chara

Posted: Wed Jun 25, 2008 12:35 pm
by Txtlocal
This post also mentions it.. no response... http://www.usenet-forums.com/php-langua ... v-bug.html

Re: fgetcsv() function not recognising GBP - british pound chara

Posted: Wed Jun 25, 2008 4:49 pm
by dml
There's a note in the documentation that might be worth following up on.
Note: 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.
So what might be happening is that the pound sign is stored in the file as one byte, but fgetcsv is expecting the file to be encoded as utf8.

There really should be an argument to fgetcsv telling it what encoding to use, but there doesn't seem to be, so to control what encoding it uses, it looks like you have to change the global locale setting.

Code: Select all

 
// Assuming that the file is encoded as ISO_8859_1/Latin1
if(setlocale(LC_CTYPE, "en_UK.ISO88591")!="en_UK.ISO88591"){
  die("couldn't set the required locale"); // just for testing
}
// now read in the csv file
 
I wouldn't be too happy about having to do this, as changing the locale affects the behaviour of all sorts of other things. If you have control of the csv file, it might be better to change its encoding to utf8, or just drop the pound sign, if it's always going to be a price in pounds in the nth column.

Re: fgetcsv() function not recognising GBP - british pound chara

Posted: Thu Jun 26, 2008 3:29 am
by samadams83
Unfortunately, never having a fixed column that currency data will fall into, the only viable solution i could find was to encapsulate the data in double-quotes.

For some reason this fixes the issue, but it does mean that i have to re-format the file to use the double-quote qualifier before every upload (using CSVed).

I guess changing the locale might be the only option if the data supplied couldn't always be changed to use the double-quotes before submission (i.e. multiple users uploading rather than just some knowledgeable administrator). I haven't tested this method though.