簡體   English   中英

編碼法語字符,csv - PHP

[英]Encoding French Characters, csv - PHP

我正在從事一個導出簡單 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); ?>

經過一番檢查后,我遇到了這個答案,我通過應用 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'));

那奏效了!

我正在開發一個導出簡單 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); ?>

使用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);

這個對我有用: iconv("UTF-8", "ISO-8859-1//TRANSLIT", $content)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM