tags:

views:

367

answers:

1

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!

+2  A: 

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