【问题标题】:PHP: Export to CSV with special charactersPHP:导出为带有特殊字符的 CSV
【发布时间】:2015-03-10 20:51:14
【问题描述】:

我正在尝试导出一些存储在表格中的数据,但是当我尝试导出到 CSV 时,此字母 č 显示为 Ä 或 č。

我尝试了所有 utf8_decode、utf8_enconde、html_entity_decode,但没有工作。我能做什么?

谢谢, 莱安德罗。

附加信息:现在我直接测试以下内容:

$delimiter = ";";
$enclosure = '"';

header("Content-Disposition: attachment; filename=memorandos.csv");
header("Pragma: no-cache");
header("Expires: 0");
$output = fopen('php://output', 'w');

$header = array('Apellido');
fputcsv($output, $header, $delimiter, $enclosure);
$memorando = Memorando::getById(3263);
if ($memorando){
    $dd = array ();
    $dd[] = $memorando->apellido;   ////ON THE DATABSE IS STORED LIKE Jurič
    fputcsv($output, $dd, $delimiter, $enclosure);
}

在文件上我看到了这个 Jurič ;而不是尤里奇

【问题讨论】:

  • 欢迎来到 Stack Overflow!这个问题的信息有点少。你能分享一下你尝试过的东西,你遇到了什么问题吗?请阅读How to ask questions on StackOverflow
  • 不要改字符,改页面\文件编码

标签: php csv export character


【解决方案1】:

处理这个问题有很多角度和方法,你甚至可以尝试:ISO-8859-1

假设你有

$input = "Fóø Bår Zacarías ?S?B?D Ferreíra"; // original text

使用 iconv 去除特殊字符

$output = iconv("utf-8", "ascii//TRANSLIT//IGNORE", $input);

Regexp 可以删除除空格之外的 utf-8 特殊字符

$output =  preg_replace("/^'|[^A-Za-z0-9\s-]|'$/", '', $output); 

结果:Foo Bar Zacarias ASABAD Ferreira

echo $output;

您的代码在哪里?可以分享一下吗?

【讨论】:

  • 我分享了我的最新测试。感谢您的回复,但使用该解决方案,字符从 č 转换为 c,我需要查看 č。谢谢。
猜你喜欢
  • 2012-05-02
  • 1970-01-01
  • 2019-09-06
  • 1970-01-01
  • 1970-01-01
  • 2012-08-19
  • 2015-09-10
  • 2014-10-28
  • 2012-10-23
相关资源
最近更新 更多