Format bei Import aus Excel (CSV) in PHP

Beim Import einer CSV-Datei die in Excel erzeugt wurde, werden häufig Umlaute und Sonderzeichen falsch dargestellt. Um die Strings umzuformatieren kommt die PHP-Funktion incov zum Einsatz.

$variable = icon('ISO-8859-1, 'UTF-8', Feld aus der CSV Datei);

Ein kompletter Import in Joomla! könnte so aussehen:

<?php        
    $dest       = JPATH_BASE.'/images/jclassroom/import/'.$fileName;
    if(($handle = fopen($dest, "r")) !== FALSE):
		$i = 1;
		while(($line = fgetcsv($handle, 0, ';', "\n")) !== false) {
			foreach($line as $key => $value) {
                if ($i === 1) {
                    $keys[$key] = $value;
                } else {
                    $out[$i][$key] = $value;
                }
            }
            $i++;
		}
	endif;
	if($out):
		foreach($out as $item):
			$return[] = array(
				'customer_number' 	=> $item[0], 
				'first_name' 	=> $item[1], 
				'last_name' 	=> $item[2],
				'company' 		=> $item[3], 
				'adress' 		=> $item[4], 
				'postcode' 		=> $item[5], 
				'city' 			=> $item[6], 
				'phone' 		=> $item[7], 
				'mobile' 		=> $item[8],
				'email' 		=> $item[9]
			);
		endforeach;
	endif;
	if($return):
		$message = '';
		foreach($return as $item):
			$check = $this->checkPeople($item['email']);
			if($check == false && $item['email'] != '') {
				JTable::addIncludePath(JPATH_ADMINISTRATOR.'/components/com_yourcomponent/tables');
				$table 		= JTable::getInstance('Student','YourcomponentTable',array());
				$data 		= array();
				$data['customer_number'] = iconv('ISO-8859-1', 'UTF-8', $item['customer_number']);
				$data['first_name'] = iconv('ISO-8859-1', 'UTF-8', $item['first_name']);
				$data['last_name'] 	= iconv('ISO-8859-1', 'UTF-8', $item['last_name']);
				$data['company'] 	= iconv('ISO-8859-1', 'UTF-8', $item['company']);
				$data['adress'] 	= iconv('ISO-8859-1', 'UTF-8', $item['adress']);
				$data['postcode'] 	= iconv('ISO-8859-1', 'UTF-8', $item['postcode']);
				$data['city'] 		= iconv('ISO-8859-1', 'UTF-8', $item['city']);
				$data['phone'] 		= iconv('ISO-8859-1', 'UTF-8', $item['phone']);
				$data['mobile'] 	= iconv('ISO-8859-1', 'UTF-8', $item['mobile']);
				$data['email'] 		= iconv('ISO-8859-1', 'UTF-8', $item['email']);
				$data['created'] 	= strval($datum);
				$data['created_by'] = $user->id;
				$data['published'] 	= 1;
				$table->bind($data);
				$table->store();
			endif;
		endforeach;
	endif;
?>