douzoudang1511 2014-03-12 03:43
浏览 43

PHP - 使用html特殊字符写入CSV

I'm trying to save data to a CSV from source data containing HTML special characters. I've tried every PHP character encoding/decoding trick I could find, nothing seems to work.

Here's a basic example of what I'm attempting without any attempt to handle the special characters. Can anyone show me what I'm doing wrong?

$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);
  • 写回答

1条回答

  • douzhuican0041 2014-03-12 04:16
    关注

    I'm not quite sure why this works but here's the workaround I discovered. If you run the array through html_entity_decode and do another html_entity_decode again on the loop it works. Hope this helps someone.

    $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);
    
    评论

报告相同问题?

悬赏问题

  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法