2

此代码将制表符分隔的 txt 文件从数据库写入目录。我的字符编码有问题,因为它无法获取 ñ , é 等非英文字符...我怎样才能正确编码?我知道我应该使用 UTF-8,但它在我的代码中的什么位置?db (latin1_swedish_ci) 中的字符似乎没问题,但文件中的字符不正常。谢谢

<?
     @chmod($export_txt, 0777);
    $fe = @fopen($export_txt."/export.txt", "w+");
    if($fe){           
        $somecontent = "";
        $fields_count = 0;           
        // fields headers
        $db->query($sql_view);
        if($row = $db->fetchAssoc()){
            foreach($row as $key => $val){
                if($fields_count++ > 0) $somecontent .= "\t";
                $somecontent .= $key;
            }
        }
        $somecontent .= "\r\n"; 

        $db->query($sql_view);
        while($row = $db->fetchAssoc()){
            $fields_count = 0;
            foreach($row as $key => $val){
                if($fields_count++ > 0) $somecontent .= "\t";
                $somecontent .= $val;                                                                          
            }
            $somecontent .= "\r\n"; 
        }  
        // write some content to the opened file.
        if (fwrite($fe, $somecontent) == FALSE) echo 'file_writing_error'." (export.txt)"; 
        fclose($fe);           

    }
    ?>
4

1 回答 1

0
fwrite($fe, utf8_encode($somecontent))
于 2012-07-04T10:55:59.440 回答