Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从Excel导入CSV时的字符编码问题?_Php_Excel_Csv_Character Encoding - Fatal编程技术网

Php 从Excel导入CSV时的字符编码问题?

Php 从Excel导入CSV时的字符编码问题?,php,excel,csv,character-encoding,Php,Excel,Csv,Character Encoding,我有一个导出CSV文件的PHP脚本。然后,我的用户在Excel中编辑文件,保存并重新上传 如果他们在字段中键入欧元符号,则在上载文件时,欧元符号以及之后的所有内容都将丢失。我正在使用str\u getcsv函数 如果我尝试转换编码(比如UTF-8),欧元符号就会消失,我会得到一个缺少的字符标记(通常由一个空白的正方形或菱形中的问号表示) 如何将编码转换为UTF-8,同时保留欧元符号(和其他非标准字符) 编辑: 这是我的密码: /** * Decodes html entity encoded

我有一个导出CSV文件的PHP脚本。然后,我的用户在Excel中编辑文件,保存并重新上传

如果他们在字段中键入欧元符号,则在上载文件时,欧元符号以及之后的所有内容都将丢失。我正在使用str\u 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_编码功能:

    iconv("Windows-1252", "UTF-8//TRANSLIT", $line);
而且还只是:

    $line = htmlentities($line, ENT_NOQUOTES, 'Windows-1252');
使用utf8_encode函数,将从字符串中删除有问题的字符。使用任何其他方法,字符和字符后面的所有内容都将丢失

示例:

字段值:“促销欧元移动”


解释为:“Promo Mobile”

将这些内容添加到CSV文件的开头

chr(239) . chr(187) . chr(191)

到目前为止你用了什么?为了帮助您,请尝试发布一些编码。在导出的CSV中输入的欧元符号字符是什么?提示:在记事本中打开csv并查看它显示的内容。这可能会破坏csv中变量的封装,或者csv需要将封装添加到其保存选项中。对我来说适用:。考虑输入CSV无效。你能把csv作为二进制文件上传到某个地方吗?好吧,csv的编码到底是什么?据我所知,编码是“Windows-1252”,不幸的是它不起作用。字段值:“Promo Eur Mobile”现在解释为:“Promo”