CSV从UTF8到ISO-8859-1(CSV from UTF8 to ISO-8859-1)

2019-09-29 08:21发布

我想修改我的CSV出口。 但它不会转换/从UTF-8我的CSV保存为ISO-8859-1

我所做出错误的任何想法,请?

实际上,因为修改此,我得到一个空的CSV文件...

(PHP的7.0.x)

function my_GenerateCSV($prefix, $csvarray, $getMonth){
    ### Generate CSV File from CSVArray
    if ($prefix == 'sepa'){ 
        $date = '-'.getTodaysDate();
    } 
    $month = $getMonth;
    $filename = 'private/'.$prefix.'-'.$month.$date.'.csv';
    $fp = fopen(ROOTDIR . '/'.$filename, 'w');
    foreach ($csvarray as $key => $value) {
        //fputcsv($fp, $value, ';');
        fputcsv($fp, convertToISOCharset($value), ';');
    }
    fclose($fp);
    return $filename;
}

function convertToISOCharset($string) {

  $string =  mb_convert_encoding($string, "ISO-8859-1", "UFT-8");
  return $string;
}

解:

function convertToISOCharset($array)
{
    foreach($array as $key => $value)
    {
        if(is_array($value))
        {
            $array[$key] = convertToISOCharset($value);
        }
        else
        {
            $array[$key] = mb_convert_encoding($value, 'ISO-8859-1', 'UTF-8');
        }
    }

    return $array;
}

Answer 1:

该解决方案的答案,因为要求:

function convertToISOCharset($array)
{
    foreach($array as $key => $value)
    {
        if(is_array($value))
        {
            $array[$key] = convertToISOCharset($value);
        }
        else
        {
            $array[$key] = mb_convert_encoding($value, 'ISO-8859-1', 'UTF-8');
        }
    }

    return $array;
}


文章来源: CSV from UTF8 to ISO-8859-1