doulongdan2264 2011-07-14 08:05
浏览 68
已采纳

PHP - fgetcsv - 引号标记在UTF-8编码的Web应用程序中输入丢失

I'm trying to import data from CSV files into a web app that uses utf-8 encoding. I'm using fgetcsv (I don't have to if there's a better way). I'm using utf8_encode to attempt to translate characters from whatever the file's encoding is. When I call mb_check_encoding on strings that come out of this particular file, I get 'ASCII'.

There are a few strange characters in the input. utf8_encode deals happily with é characters (where before they were coming out as black diamond question marks). However, it fails to translate double quotes and apostrophes, and instead just removes them.

Help much appreciated, thanks. I'm using CakePHP, in case that gives me some more options!

Edit - I meant utf8_encode, not utf8_decode.

  • 写回答

2条回答 默认 最新

  • doq8211 2011-07-14 09:53
    关注

    You only need one call to iconv with the correct charset for the $in_charset parameter.

    $utf8Text = iconv($inputCharset, 'UTF-8', $text);
    

    You need to know the input charset. There's no way around it. Make a specification that all input needs to be in ISO-8859-1, or whatever you prefer. Alternatively, find out what the charset of your input is (ask the author, test yourself in an editor, whatever). Alternatively, require that the input needs to specify what encoding it's in somewhere, somehow.

    Encoding is not black magic. You just need to be aware of what encoding some text is in and what encoding you want it to be in. Then use a function like iconv that can cleanly translate the characters from one encoding to another. utf8_encode and utf8_decode translate between ISO-8859-1 and UTF-8. Their names are chosen terribly, since they suggest they can automagically translate anything from and to UTF-8, but that's not the case.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退