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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?