【问题标题】:PHP - write to CSV with html special charactersPHP - 使用 html 特殊字符写入 CSV
【发布时间】:2014-03-12 03:43:00
【问题描述】:

我正在尝试将包含 HTML 特殊字符的源数据中的数据保存到 CSV。我已经尝试了所有我能找到的 PHP 字符编码/解码技巧,但似乎没有任何效果。

这是一个基本示例,说明我没有尝试处理特殊字符。谁能告诉我我做错了什么?

$file = fopen("output.csv", "w");
fwrite($file, '');

$array = array(
    'Casa e Decoração,queimadeestoque20,Zanox',
    'eFácil, Cupom de até 10% de desconto na eFácil, Asus'
);

foreach ($array as $line) {
    $line = explode(',',$line);
    fputcsv($file,$line);
}


fclose($file);

【问题讨论】:

  • 您是否尝试将这些 $line 放在双引号 (") 之间?
  • fputcsv($file,$line,','," "); - 不,不幸的是没有工作。

标签: php csv character-encoding htmlspecialchars


【解决方案1】:

我不太确定为什么会这样,但这是我发现的解决方法。如果您通过html_entity_decode 运行数组并在循环上再次执行另一个html_entity_decode,则它可以工作。希望这对某人有所帮助。

$file = fopen("output.csv", "w");
fwrite($file, '');

$array = array(
    'Casa e Decoração,queimadeestoque20,Zanox',
    'eFácil, Cupom de até 10% de desconto na eFácil, Asus'
);

$whatever = array();
foreach ($array as $line) {
    $line = html_entity_decode($line);
    $whatever[] = $line;
}

foreach ($whatever as $line) {
    $line = html_entity_decode($line);

    $line = explode(',',$line);
    fputcsv($file,$line);
}

fclose($file);

【讨论】:

    猜你喜欢
    • 2021-05-26
    • 2021-11-21
    • 1970-01-01
    • 1970-01-01
    • 2012-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-26
    相关资源
    最近更新 更多