关闭
dqeznd1697 2015-03-03 09:34
浏览 204
已采纳

将csv文件处理为UTF-8

Trying to figure out how to process a csv file with UTF encoding. Tried multiple ways like adding this utf8_encode() and with this in the header:

header('Content-Type: text/html; charset=UTF-8');

But nothing seems to work.

The code is:

<?php
include 'head.php';
$csv = array_map("str_getcsv", file("translations/dk.csv"));
foreach ($csv as $line){
     $translate["dk"][ $line[0] ] = $line[1];
}if ($line[1] != NULL){
    $line[0] = $line[1];
}
echo $line[0];
fclose($csv);
?>

How to I echo the output with UTF-8 encoding?

  • 写回答

2条回答 默认 最新

  • douhui1630 2015-03-03 09:43
    关注

    When you would display it in a browser you should use valid html and set the meta charset to utf8 too:

    <?php
    include 'head.php';
    ?>
    <!DOCTYPE html>
    <html lang="dk">
    <head>
        <meta charset="utf-8"/>
    </head>
    <body>
    <?php
    
    $csv = array_map("str_getcsv", file("translations/dk.csv"));
    foreach ($csv as $line){
         $translate["dk"][ $line[0] ] = $line[1];
    }if ($line[1] != NULL){
        $line[0] = $line[1];
    }
    echo $line[0];
    fclose($csv);
    ?>
    </body>
    </html>
    

    Or using text/plain instead of text/html can help:

    header('Content-Type: text/plain; charset=UTF-8');
    

    Hope that helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部