【问题标题】:Encoding French Characters, csv - PHP编码法语字符,csv - PHP
【发布时间】:2017-01-11 19:20:19
【问题描述】:

我正在开发一个导出简单 CSV 的项目。到目前为止,只要我使用英文字符,我的代码就可以工作并生成 CSV。

一些列名将使用法语并希望能够对其进行编码。

这是我所拥有的:

<?php 
    // open the file "demosaved.csv" for writing
    $file = fopen('myfrench-export.csv', 'w');
    // save the column headers
    fputcsv($file, array('Question', 'Réponse 1/4', 'Réponse 2/4', 'Réponse 3/4', 'Réponse 4/4', 'Total completé', 'Total non-completé', 'Total cumulatif'));

    // Sample data. This can be fetched from mysql too
    $data = array(
        array('Data 11', 'Data 12', 'Data 13', 'Data 14', 'Data 15'),
        array('Data 21', 'Data 22', 'Data 23', 'Data 24', 'Data 25'),
        array('Data 31', 'Data 32', 'Data 33', 'Data 34', 'Data 35'),
        array('Data 41', 'Data 42', 'Data 43', 'Data 44', 'Data 45'),
        array('Data 51', 'Data 52', 'Data 53', 'Data 54', 'Data 55'),
        array('Data 51', 'Data 52', 'Data 53', 'Data 54', 'Data 55'),
        array('Data 51', 'Data 52', 'Data 53', 'Data 54', 'Data 55'),
        array('Data 51', 'Data 52', 'Data 53', 'Data 54', 'Data 55'),
    );

    // save each row of the data
    foreach ($data as $row)
    {
        fputcsv($file, $row);
    }

    // Close the file
    fclose($file); ?>

【问题讨论】:

  • 是什么导致法语文本被编码为 "Réponse 3/4" 而不是 "Réponse 3/4" ,以及如何解决?
  • 您在什么操作系统上编写代码?你的代码在什么操作系统上运行?
  • 我在 Windows 10 上编写代码,代码在 Xampp 上本地运行,在 Wordpress 应用程序上的 Azure 上远程运行。不过这有关系吗?
  • 使用 mb_convert_encoding( $string, 'HTML-ENTITIES','utf-8' ); 为我工作;而utf8_decode 并没有做到这一点。

标签: php csv


【解决方案1】:

经过一番检查,我发现了这个答案,我通过应用 utf8_decode 调整了这一行,并且成功了。

基本上我换了

fputcsv($file, array('Question', 'Réponse 1/4', 'Réponse 2/4', 'Réponse 3/4', 'Réponse 4/4', 'Total completé', 'Total non-completé', 'Total cumulatif'));

$array = array_map("utf8_decode", array('Question', 'Réponse 1/4', 'Réponse 2/4', 'Réponse 3/4', 'Réponse 4/4', 'Total completé', 'Total non-completé', 'Total cumulatif'));

这很有效!

【讨论】:

  • 没用,大数据集呢??
  • 相应映射
  • 我在想这段代码很有用,但最后没有用。很遗憾,我无法撤消我的投票。
【解决方案2】:

使用 bom 允许在 excel 中显示特殊字符。

这段代码可以帮助你:

// Open the file "demosaved.csv" for writing
$file = fopen('myfrench-export.csv', 'w');

// Mandatory: Use bom to allow to display special characters with excel
fwrite($file, $bom = chr(hexdec('EF')).chr(hexdec('BB')).chr(hexdec('BF')));

// Save the column headers
fputcsv($file, array('Question', 'Réponse 1/4', 'Réponse 2/4', 'Réponse 3/4', 'Réponse 4/4', 'Total completé', 'Total non-completé', 'Total cumulatif'));

// Sample data. This can be fetched from mysql too
$data = array(
    array('Data 11', 'Data 12', 'Data 13', 'Data 14', 'Data 15'),
    array('Data 21', 'Data 22', 'Data 23', 'Data 24', 'Data 25'),
    array('Data 31', 'Data 32', 'Data 33', 'Data 34', 'Data 35'),
    array('Data 41', 'Data 42', 'Data 43', 'Data 44', 'Data 45'),
    array('Data 51', 'Data 52', 'Data 53', 'Data 54', 'Data 55'),
    array('Data 51', 'Data 52', 'Data 53', 'Data 54', 'Data 55'),
    array('Data 51', 'Data 52', 'Data 53', 'Data 54', 'Data 55'),
    array('Data 51', 'Data 52', 'Data 53', 'Data 54', 'Data 55'),
);

// Save each row of the data
foreach ($data as $row)
{
    fputcsv($file, $row);
}

// Close the file
fclose($file);

【讨论】:

    猜你喜欢
    • 2013-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多