Some characters in CSV file are not read during PHP fgetcsv()

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!

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

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)
...

Solution 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);

Solution 3

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

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply