CSV从UTF8到ISO-8859-1

时间:2017-09-08 13:56:56

标签: php csv utf-8 iso-8859-1

我正在尝试修改我的CSV导出。 但它不会将我的CSV从UTF-8转换/保存到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;
}

1 个答案:

答案 0 :(得分: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;
}