dongyuchen0214 2012-10-19 03:22
浏览 31

无法读取csv中的连字符值

While reading hyphenated values from a csv file, I see the hyphen is gets converted into â€

My csv file contains data such as :

05‐30‐2012,user,500 
06‐30‐2012,user,1500 

After reading the lines I get :

05â€30â€2012,user,500 
06â€30â€2012,user,1500 

Why is it happening? How do I correct it ?

This is what I have done :

$lines = file('data.csv');  

foreach($lines as $line_num => $line){
  if($line != ''){

  echo '<pre>';
  print_r($line);       
  echo '</pre>';
}

Thanks in advance.

  • 写回答

1条回答 默认 最新

  • douyinliu8813 2012-10-19 04:08
    关注

    If your output encoding is not UTF-8, then you need to convert your UTF-8 input file to your output encoding. For example, assuming your output to the browser should be ISO-8859-1 (a common default with Apache setups), then utf8_decode will convert your UTF-8 CSV data into the proper encoding.

    $lines = file('data.csv');  
    
    foreach($lines as $line_num => $line){
      if($line != ''){
    
      echo '<pre>';
      print_r(utf8_decode($line));
      echo '</pre>';
    }
    

    If you want UTF-8 output (or if you are fine with that), set the default_charset PHP setting as early in your script as possible so PHP will set the correct HTTP header.

    ini_set('default_charset','UTF-8');
    
    评论

报告相同问题?

悬赏问题

  • ¥15 对于这个复杂问题的解释说明
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败