PHP导入带有utf-8重音的CSV


PHP import CSV with utf-8 accents

我在导入CSV文件时遇到问题,其中包含带有口音的(法语)名称…当它们被导入时,重音不能正确显示,例如

fsamlix变成fŽlix

文件是手工创建的,然后导入到PHP中。

我已经尝试了utf8_encode()和utf8_decode(),两个函数都不会转换字符,因此它们可以正确查看。

我的问题是我如何才能得到这个正确渲染…转换char-set . .等

根据我在这里看到的其他问题,我相信文本是用Cp850编码的。我使用fgetcvs()来获取内容。

在以UTF格式输出前设置报头信息

header('Content-Type: text/html; charset=utf-8');
$log = file_get_contents("log.csv");
echo utf8_encode($log);

输出
félix

请尝试iconv()函数

我认为这是一个迟来的答案,但可能对那些仍在寻找解决方案的人有所帮助。这只是一个微调。不推荐。

header('Content-Encoding: UTF-8');
header('Content-type: text/csv; charset=UTF-8');
header('Content-Disposition: attachment; filename=filename.csv');
echo "'xEF'xBB'xBF"; // UTF-8 with BOM
readfile("filename.csv");
exit;

我在上传时这样做

if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_dir .$target_file)) {
      $log = file_get_contents($target_dir .$target_file);
      file_put_contents($target_dir .$target_file, utf8_encode($log));