CSV from UTF8 to ISO-8859-1

时光毁灭记忆、已成空白 提交于 2019-12-24 10:42:17

问题


i am trying to modify my CSV export. But it wont convert / save my CSV from UTF-8 to ISO-8859-1

Any Ideas what i have made wrong, please?

Actually since modifying this, i get a empty CSV File...

(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;
}

SOLUTION:

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:


The Solution as answer, as requested:

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;
}


来源:https://stackoverflow.com/questions/46118202/csv-from-utf8-to-iso-8859-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!