note 85965 deleted from function.fgets by danbrown
| From: | danbrown@php.net | Date: | Fri, 26 Sep 2008 13:51:28 +0000 |
| Subject: | note 85965 deleted from function.fgets by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-144747@lists.php.net to get a copy of this message | ||
Note Submitter: Simone mailto:
c2ltb3BocEBub3R0ZS5jb20=
----
my email above ^^^^^^ should be base64 decoded
fgets and fgetcsv have a lot of problems with unicode files. it's safest to use fread, but the
problem is that since a line hasn't a fixed lenght (in bytes), how to avoid to truncate a line?
this is my solution:
$folder='www.notte.com';
$handle = fopen("/tmp/$folder/file.csv", "r");
$tmpbuffer =''; //to store half lines
while (!feof($handle) || $tmpbuffer) {
$buffer = $tmpbuffer.fread($handle,100*1024);
$buffer=iconv ('unicode','UTF-8',$buffer);
$rows=explode("\n",$buffer);
$countrows=count($rows);
$tmpbuffer='';
if($countrows > 1 ){
$tmpbuffer=iconv ('UTF-8','unicode',$rows[$countrows -1 ]);
unset($rows[$countrows -1 ]);
}
foreach($rows as $row){
echo "\nCORRECT ROW:$row";
//now i can parse this row as a csv
}
}