catinuk Posted November 2, 2009 Share Posted November 2, 2009 I wish to load some Chinese characters into a csv file, however the following code only works with English words, anyone knows how to change the code so it works with Chinese characters? Many thanks, Cat $pfw_file_name = "registration.csv"; $pfw_first_row = "title,First Name,Last Name,Job Title,Company,Email,Telephone Number,Fax Number,Address 1,Address 2,City,County,Postcode,Country,ID,Other\r\n"; //Content-type: text/html; charset=utf-8\r\n $pfw_values = "$title,$firstName,$lastName,$jobTitle,$company,$email,$tel,$fax,$address1,$address2,$city,$county,$postcode,$country,$id,$other\r\n"; $pfw_is_first_row = false; if(!file_exists($pfw_file_name)) { $pfw_is_first_row = true; } if (!$pfw_handle = fopen($pfw_file_name, 'a+')) { die("Cannot open file ($pfw_file_name)"); exit; } if ($pfw_is_first_row) { if (fwrite($pfw_handle, $pfw_first_row ) === FALSE) { die("Cannot write to file ($pfw_filename)"); exit; } } if (fwrite($pfw_handle, $pfw_values) === FALSE) { die("Cannot write to file ($pfw_filename)"); exit; } fclose($pfw_handle); Quote Link to comment Share on other sites More sharing options...
MadTechie Posted November 2, 2009 Share Posted November 2, 2009 read about utf8_encode in the manual should help This function encodes the string data to UTF-8, and returns the encoded version. Quote Link to comment Share on other sites More sharing options...
catinuk Posted November 3, 2009 Author Share Posted November 3, 2009 read about utf8_encode in the manual should help This function encodes the string data to UTF-8, and returns the encoded version. I tried, but it seems doesn't change anything. i can view the Chinese characters in notepad, but if i open it in Excel, it becomes funny code Quote Link to comment Share on other sites More sharing options...
Mchl Posted November 3, 2009 Share Posted November 3, 2009 You have to tell Excel to open it as UTF8. Don't ask me how. Open Office's Calc asks when opening CSV files. Excel doesn't. Quote Link to comment Share on other sites More sharing options...
MadTechie Posted November 3, 2009 Share Posted November 3, 2009 You could try adding a UTF-8 header to the file.. that will atleast make Excel prompt you for the correct format try adding fwrite($pfw_handle, "\xEF\xBB\xBF", 3); inside your if ($pfw_is_first_row) loop Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.