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);
dpmpa26468
dpmpa26468 如fputcsv($file,$line,',',“”);-遗憾的是没有那个不起作用。
6 年多之前 回复
dounang1974
dounang1974 你试图将这些$line放在双引号(“)之间吗?
6 年多之前 回复

1个回答

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);
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐