1

I wrote a PHP script that connects to a distributor's server, downloads several inventory files, and creates a massive .csv file to import into WooCommerce. Everything works except for one thing: when I look at the exported .csv file, the "x" character in my "caliber" column is always converted to the string "×".

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...

The caliber "9×23mm Winchester" is displayed as "9×23mm Winchester" in the .csv file. I've tried placing single quotes around the array key and escaping the character "x". There are multiple instances of this mysterious switch.

Thanks in advance for any help!

2 Answers2

0

Try to put header on top of your script:

header('Content-Type: text/html; charset=utf-8');
McBern
  • 549
  • 1
  • 4
  • 8
0

This is an encoding issue. The character "×" is incorrectly encoded from UTF-8 to ISO-8859-1. Specify the output encoding as UTF-8, for example header('Content-Type: text/html; charset=utf-8');, or manually specify encoding in your browser will solve this issue.

"×" is U+C397, and code point C3 in ISO-8859-1 is tilde A "Ã".

Geno Chen
  • 4,916
  • 6
  • 21
  • 39