dqeznd1697 2015-03-03 17:34
浏览 203
已采纳

将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 17: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条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 MATLAB动图问题
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名