0

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.

maan81
  • 3,469
  • 8
  • 36
  • 53

1 Answers1

0

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');
jimp
  • 16,999
  • 3
  • 27
  • 36