dousi1097 2013-09-11 12:27
浏览 99
已采纳

如何在读取CSV文件时修复编码?

I'm reading a CSV file in php and so far as i have understood - these kind of files can have any encoding that was ever invented by hoomans omg and so on... i guess i have a MacRoman ANSI encoded CSV, i'm working on a Mac.

So far, so good (not good at all but thats another topic).. Now, while iterating through the lines, i have a value like:

Z�rich

Obviously, it should be "Zürich" - the "ü" is missing..

Now, i have tried almost anything.. mb_detect_encoding is saying "false" so, he doesn't understand what it is...

Then i have found a genius class by Sebastian Grignoli here -> Detect encoding and make everything UTF-8

Seems nice but... all i got is:

ZŸrich

not really the "ü" i have expected :D

Now i have found out, that a "utf8_encode" will work somehow, it generates:

Z\u009Frich

but.. what now? if i put this directly in the database, the final value is "Zrich", which means it is still not really UTF-8, or is the db just struggling with the escaped variant? When i make an mb_detect_encoding on that value, he says now "UTF-8".. nice.. but how can i go further? How can i get my "Zürich" the right way in UTF-8?

  • 写回答

2条回答 默认 最新

  • douchen3562 2013-09-11 21:05
    关注

    You can probably use iconv for the conversion. On my installation, the MacRoman encoding is called simply "MAC":

    $city = "Z\x9frich";
    $city = iconv("MAC", "UTF-8", $city); 
    echo $city; // Output: Zürich
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程