为什么在 运行 我的 PHP 脚本之后将字符 "x" 放入 .csv 文件中时变为“×”?

Why does the character "x" change to "×" when placed into a .csv file after running my PHP script?

我写了一个 PHP 连接到分销商服务器的脚本,下载了几个库存文件,并创建了一个巨大的 .csv 文件以导入到 WooCommerce 中。除一件事外一切正常:当我查看导出的 .csv 文件时,"caliber" 列中的 "x" 字符始终转换为字符串“×”。

updateInventoryFunctions.php:

function fixCalibers($number, $attributesList) {

    $calibers = array (
        # More calibers...
        "9×23mm Winchester" => '9X23'
    );

    $pos = array_search($attributesList[$number], $calibers);
    if ($pos !== false) {
        $attributesList[$number] = $pos;
        return $attributesList[$number];
    } elseif ($attributesList[$number] == "40SW") {
        $attributesList[$number] = '.40 S&W';
        return $attributesList[$number];
    } # More conditionals...

}

updateInventory.php:

# Code that connects to the distributor's server, downloads files, and places headers into the .csv file.

if (($localHandle = fopen("current_rsr_inventory.csv", "w")) !== false) {

    # Code that defines arrays for future fixes and creates a multidimensional array of attributes...

    foreach ($tempInventoryFile as &$line) {

        $line = explode(";", $line);

        # Code that fixes several inconsistencies from the distributor...

        $matchingKey = array_search($line[0], $skuList);
        $attributesList = array();
        if ($matchingKey !== false) {

            # Code that fixes more inconsistencies...

            if ($attributesList[18] === "" || $attributesList[18] === null) {

                array_splice($attributesList, 18, 1);

                include_once "updateInventoryFunctions.php";
                $attributesList[17] = fixCalibers(17, $attributesList);

            } # More conditionals...

            # Code that fixes more inconsistencies...

            foreach ($attributesList as $attribute) {
                $line[] = $attribute;
            } // End foreach.

        } // End if.

        fputcsv($localHandle, $line);

    } // End foreach.

} // End if.

# Code that closes files and displays success message...

口径“9×23mm Winchester”在 .csv 文件中显示为“9×23mm Winchester”。我试过在数组键周围放置单引号并转义字符 "x"。这个神秘开关有多个实例。

在此先感谢您的帮助!

尝试将 header 置于您的脚本之上:

header('Content-Type: text/html; charset=utf-8');

这是一个编码问题。字符“×”被错误地从 UTF-8 编码为 ISO-8859-1。将输出编码指定为 UTF-8,例如 header('Content-Type: text/html; charset=utf-8');,或在浏览器中手动指定编码将解决此问题。

"×" 是 U+C397ISO-8859-1 中的代码点 C3 是波浪线 A "Ã"。