4

I am reading a CSV file with php. Many of the rows have a "check mark" which is really the square root symbol: √ and the php code is just skipping over this character every time it is encountered.

Here is my code (printing to the browser window in "CSV style" format so I can check that the lines break at the right place:

$file = fopen($uploadfile, 'r');
while (($line = fgetcsv($file)) !== FALSE) {
   foreach ($line as $key => $value) {
    if ($value) {
       echo $value.",";
    }
   }
   echo "<br />";
}
fclose($file);

As an interim solution, I am just finding and replacing the checkmarks with 1's manually, in Excel. Obviously I'd like a more efficient solution :) Thanks for the help!

Jen
  • 361
  • 2
  • 6
  • 16
  • I've had plenty of problems using `fgetcsv()` in the past. When I run into these types of issues I usually start parsing the file manually. =\ – pix0r Sep 24 '09 at 16:48
  • There are known bugs with fgetcsv() and non-ASCII characters, particularly at the beginning of unquoted values. See also http://stackoverflow.com/questions/2238971/fgetcsv-ignores-special-characters-when-they-are-at-the-beginning-of-line – eswald Jan 26 '12 at 20:13

3 Answers3

4

fgetcsv() only works on standard ASCII characters; so it's probably "correct" in skipping your square root symbols. However, rather than replacing the checkmarks manually, you could read the file into a string, do a str_replace() on those characters, and then parse it using fgetcsv(). You can turn a string into a file pointer (for fgetcsv) thusly:

$fp = fopen('php://memory', 'rw');
fwrite($fp, (string)$string);
rewind($fp);
while (($line = fgetcsv($fp)) !== FALSE)
...
2

I had a similar problem with accented first characters of strings. I eventually gave up on fgetscv and did the following, using fgets() and explode() instead (I'm guessing your csv is comma separated):

$file = fopen($uploadfile, 'r');

while (($the_line = fgets($file)) !== FALSE)  // <-- fgets
{
  $line = explode(',', $the_line);            // <-- explode
  foreach ($line as $key => $value) 
  {
    if ($value) 
    {
      echo $value.",";
    }
  }
  echo "<br />";
}

fclose($file);
Pabbles
  • 101
  • 3
  • I have a question about this solution: http://stackoverflow.com/questions/14836653/php-fgetcsv-not-reading-first-utf-8-caracter-alternatif-using-fgets – StiGMaT Feb 12 '13 at 16:16
0

You should setlocale ar written in documentation

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.

before fgetcsv add setlocale(LC_ALL, 'en_US.UTF-8'). In my case it was 'lt_LT.UTF-8'.

This behaviour is reported as a php bug

Aurimas
  • 770
  • 9
  • 14