我有一個導出CSV文件的PHP腳本。我的用戶然後在Excel中編輯文件,保存並重新上傳。從Excel導入CSV時的字符編碼問題?
如果他們在字段中輸入歐元符號,文件上傳時,歐元符號以及之後的所有內容都會丟失。我正在使用str_getcsv函數。
如果我嘗試轉換編碼(比如說UTF-8),歐元符號消失,並且我得到一個缺失的字符標記(通常由一個空白方塊或鑽石中的問號表示)。
如何將編碼轉換爲UTF-8,還要保留歐元符號(以及其他非標準字符)?
編輯:
這裏是我的代碼:
/**
* Decodes html entity encoded characters back to their original
*
* @access public
* @param String The element of the array to process
* @param Mixed The key of the current element of the array
* @return void
*/
public function decodeArray(&$indexValue, $key)
{
$indexValue = html_entity_decode($indexValue, ENT_NOQUOTES, 'Windows-1252');
}
/**
* Parses the contents of a CSV file into a two dimensional array
*
* @access public
* @param String The contents of the uploaded CSV file
* @return Array Two dimensional-array.
*/
public function parseCsv($contents)
{
$changes = array();
$lines = split("[\n|\r]", $contents);
foreach ($lines as $line) {
$line = utf8_encode($line);
$line = htmlentities($line, ENT_NOQUOTES);
$lineValues = str_getcsv($line);
array_walk($lineValues, 'decodeArray');
$changes[] = $lineValues;
}
return $changes;
我也曾嘗試以下,而不是函數utf8_encode功能:
iconv("Windows-1252", "UTF-8//TRANSLIT", $line);
而且也只是:
$line = htmlentities($line, ENT_NOQUOTES, 'Windows-1252');
使用utf8_encode函數,將從字符串中刪除違規字符。使用任何其他方法,角色和角色後面的所有內容都會丟失。
例子:
的字段值: 「促銷€移動」
被解釋爲: 「促銷手機」
你到目前爲止使用了什麼?嘗試發佈一些使用代碼以幫助您。 – DannyG
歐元符號的什麼字符被放入導出的CSV文件中?提示:打開記事本中的csv並查看它顯示的內容。它可能是這樣的,它打破了csv中的變量封裝或者csv需要封裝添加到它的保存選項。 – Dave
適合我:http://3v4l.org/9b3s3。 將輸入CSV視爲無效。你能把csv上傳到二進制文件嗎? – floww