[英]Character Encoding Error in File
I have a input where the user can (and has and will always) paste data in from MSword; 我有一个输入,用户可以(并且一直并且将始终)从MSword中粘贴数据; in particular characters like ©
特别是©
字符 ®
™
When I add the value to the database system using html_entity_decode($foo, ENT_NOQUOTES, 'UTF-8');
当我使用html_entity_decode($foo, ENT_NOQUOTES, 'UTF-8');
将值添加到数据库系统时html_entity_decode($foo, ENT_NOQUOTES, 'UTF-8');
When I read from the database system it renders on the screen fine, but when I write the data to a file (using fputscsv
) and attach that to an email the resulting csv file (opened in Excel) it comes out like Copyright© . 当我从数据库系统中读取数据时,它可以在屏幕上正常显示,但是当我将数据写入文件(使用fputscsv
)并将其附加到电子邮件中时,得到的csv文件(在Excel中打开)会像版权©一样出现 。
Any idea on how to get it in the right format for the file? 关于如何以正确的文件格式获取文件的任何想法?
Try writing byte order mark to the file before writing CSV string. 尝试在写入CSV字符串之前将字节顺序标记写入文件。
$byte_order_mark = "\xEF\xBB\xBF";
$contents = $byte_order_mark . $csv_string;
file_put_contents( $path_to_csv_file );
Solution found; 找到解决方案; it turned out to be a character encoding problem (I'm blaming Microsoft, as it is fine when you open the file in Notepad++). 原来是字符编码问题(我怪微软,因为在Notepad ++中打开文件就可以了)。
When adding the data to the file convert the character encoding of the data using iconv . 将数据添加到文件时,请使用iconv转换数据的字符编码。
Example; 例;
foreach ($data_row as $k => $cell) {
$data_row[$k] = iconv("UTF-8", "ISO-8859-1//TRANSLIT", $cell);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.