PHP-将Unicode转换为CSV,例如汉字
通过表单post,我们将数据传递给PHP脚本,该脚本为电子表格程序(如excel)创建可下载的CSV。 如果没有传递特殊字符,下面的scipt工作正常。但传递特殊字符(如中文字母)以unicode结尾 例如: 我们发送“你好世界" (hello world,unicode:你;好;世;界;)通过表单字段发布到这个php脚本export.phpPHP-将Unicode转换为CSV,例如汉字,php,excel,csv,unicode,Php,Excel,Csv,Unicode,通过表单post,我们将数据传递给PHP脚本,该脚本为电子表格程序(如excel)创建可下载的CSV。 如果没有传递特殊字符,下面的scipt工作正常。但传递特殊字符(如中文字母)以unicode结尾 例如: 我们发送“你好世界" (hello world,unicode:你;好;世;界;)通过表单字段发布到这个php脚本export.php <?php //opens a download dialog, download by excel
<?php //opens a download dialog, download by excel
header("Content-type: text/csv; charset=UTF-8");
header("Content-Disposition: attachment; filename=\"eCalc Results.csv\"");
echo html_entity_decode(urldecode($_POST["exportData"]));
// returns unicode 你好世界
?>
如何将此unicodes转换为CSV,使中文字母再次正确显示为你好世界...?
下面是一个例子:
我们搜索了整个stackoverflow,但没有成功转换
谢谢你的帮助
eCalc您可以查看PHP文档的这一部分:…找到了一个解决方案: 首先,不使用javascript encode()对发送方进行编码,而是使用encodeURI()并使用
在标题中
…这里是export.php(插入BOM!!)
csv文件中需要中文字母,对吗?如果需要,我在服务器上测试了该代码,我可以在csv文件中看到中文字符
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />
<?php
header("Content-Encoding: UTF-8");
header("Content-Disposition: attachment; filename=\"eCalc Results.csv\"");
header("Content-type: text/csv; charset=UTF-8");
echo "\xEF\xBB\xBF"; // first send the UTF-8 BOM for Excel
echo html_entity_decode(urldecode($_POST["exportData"]));
?>