fgetcsv() function not recognising GBP - british pound chara

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
samadams83
Forum Newbie
Posts: 2
Joined: Wed Jun 18, 2008 5:49 am

fgetcsv() function not recognising GBP - british pound chara

Post 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
Txtlocal
Forum Newbie
Posts: 3
Joined: Wed Jun 25, 2008 12:26 pm

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

Post 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.
Txtlocal
Forum Newbie
Posts: 3
Joined: Wed Jun 25, 2008 12:26 pm

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

Post 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...
Txtlocal
Forum Newbie
Posts: 3
Joined: Wed Jun 25, 2008 12:26 pm

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

Post by Txtlocal »

This post also mentions it.. no response... http://www.usenet-forums.com/php-langua ... v-bug.html
dml
Forum Contributor
Posts: 133
Joined: Sat Jan 26, 2008 2:20 pm

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

Post 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.
samadams83
Forum Newbie
Posts: 2
Joined: Wed Jun 18, 2008 5:49 am

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

Post 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.
Post Reply