csv中的奇数字符为英镑符号

时间:2016-05-05 16:50:50

标签: php wordpress csv character-encoding symbols

我已经编写了一个函数(在wordpress中)来下载csv文件,如下所示。

function download_csv(){

if( !empty($_GET['filename']) && !empty($_GET['downloadname']) && file_exists( trailingslashit( wsoe_upload_dir() ).$_GET['filename'].'.csv' ) && wsoe_is_shop_manager() ) {

    $download_filename = $_GET['downloadname'];
    $filename   = trailingslashit( wsoe_upload_dir() ).$_GET['filename'].'.csv';
    $charset = get_option('blog_charset');
    $settings = self::advanced_option_settings();

    $file = fopen( $filename, 'r' );
    $contents = fread($file, filesize($filename));
    fclose($file);

    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header('Content-Description: File Transfer');
    header('Content-Encoding: '. $charset);
    header('Content-type: text/csv; charset='. $charset);
    header("Content-Disposition: attachment; filename=$download_filename.csv");
    header("Expires: 0");
    header("Pragma: public");

    $fh = @fopen( 'php://output', 'w' );

    if( !empty($settings['wsoe_fix_chars']) ){

        /**
         * This is a fix for Microsoft Excel. It may happen that some weird characters
         * may appear while viewing the csv on excel with MAC OS.
         */

        $contents = mb_convert_encoding( $contents, 'UTF-16LE', $charset );
        $contents = chr(255) . chr(254).$contents; // Add byte order mark
    }

    fwrite( $fh, $contents );
    fclose($fh);
    exit();

   }
}

当csv导出时,井号(£)在查看时无法正常显示。我已在所有编辑器中检查过它,包括Excel,Notepad ++,OpenOffice等。

看起来像这样

Currency symbol appears to be junk character

有人可以告诉我如何解决这个问题吗?

0 个答案:

没有答案