samadams83 Posted June 18, 2008 Share Posted June 18, 2008 I am trying to get data from a CSV file using the fgetcsv 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: 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: 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? Link to comment https://forums.phpfreaks.com/topic/110747-fgetcsv-function-not-recognising-gbp-british-pound-character/ Share on other sites More sharing options...
rarebit Posted June 18, 2008 Share Posted June 18, 2008 I tried this, but it does seem to be csv or in a newer version than i'm using... However have a look at the first comment here Link to comment https://forums.phpfreaks.com/topic/110747-fgetcsv-function-not-recognising-gbp-british-pound-character/#findComment-568257 Share on other sites More sharing options...
rarebit Posted June 18, 2008 Share Posted June 18, 2008 $handle = fopen("mycsv.csv", "r"); while ($line = fgets($handle)) { $row = preg_split( "/[\s,]*\\\"([^\\\"]+)\\\"[\s,]*|[\s,]+/", $line, 0, PREG_SPLIT_DELIM_CAPTURE ); print_r($row); print "<br>"; } fclose($handle); Link to comment https://forums.phpfreaks.com/topic/110747-fgetcsv-function-not-recognising-gbp-british-pound-character/#findComment-568262 Share on other sites More sharing options...
samadams83 Posted June 19, 2008 Author Share Posted June 19, 2008 Thanks for that, very useful. Now all i need to do is understand that regex! I also found another less universal solution: if an enclosure (e.g. ") is used on the data being read, the £ sign will show up correctly. Bug in PHP? Link to comment https://forums.phpfreaks.com/topic/110747-fgetcsv-function-not-recognising-gbp-british-pound-character/#findComment-569010 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.