Encoding Problem

Topics: Developer Forum, User Forum
Aug 21, 2009 at 4:21 PM

Hi Guys,

I have a Problem with the encoding i think, because when i import csv files i get all values to an array which looks like that:

  1 => 
array
0 => <small>string</small> 'Max ' (length=50)
1 => <small>string</small> 'Mustermann ' (length=25)
2 => <small>string</small> '198070' (length=6)
3 => <small>string</small> '13.05.2009' (length=10)
4 => <small>string</small> 'xxxxxxxxxxx' (length=18)
5 => <small>string</small> '09.08.2009' (length=10)
6 => <small>string</small> '115,45' (length=6)
7 => <small>string</small> 'j' (length=1)
8 => <small>string</small> 'MAKxxxxxx ' (length=25)
9 => <small>string</small> '3000' (length=4)
10 => <small>string</small> 'Angebot' (length=7)
11 => <small>string</small> '146186' (length=6)

but i need this value (see 7. Element):

  1 => 
array
0 => <small>string</small> 'Max ' (length=50)
1 => <small>string</small> 'Mustermann ' (length=25)
2 => <small>string</small> '198070' (length=6)
3 => <small>string</small> '13.05.2009' (length=10)
4 => <small>string</small> 'xxxxxxxxxxx' (length=18)
5 => <small>string</small> '09.08.2009' (length=10)
6 => <small>string</small> '115,45' (length=6)
7 => <small>string</small> 'jährlich' (length=8)
8 => <small>string</small> 'MAKxxxxxx ' (length=25)
9 => <small>string</small> '3000' (length=4)
10 => <small>string</small> 'Angebot' (length=7)
11 => <small>string</small> '146186' (length=6)

 

 

Can Someone please help me, because i don't know what to do?

 

Thanks max

 

		// Überprüfen ob es sich um ein CSV File handelt
		$dateityp = substr(strtolower($_FILES[$this->name_dateiupload_feld]['name']), -4);
		if($dateityp == ".csv"){
			// Reader initalisieren
			$objReader = PHPExcel_IOFactory::createReader('CSV')
    			->setDelimiter("$this->import_csv_delimiter")
    			->setEnclosure("$this->import_csv_enclosure")
    			->setLineEnding("$this->import_csv_lineending")
    			->setSheetIndex("$this->import_sheetindex");
    			
			// Überprüfen ob alle CSV  Parameter gesetzt sind
			if($objReader->getDelimiter() != ";" && $objReader->getDelimiter() != ","){
				$this->set_error(19);
			} elseif($objReader->getSheetIndex() == "" && $objReader->getSheetIndex() < 0){
				$this->set_error(20);
			} elseif($objReader->getLineEnding() == ""){
				$this->set_error(21);
			}
		} else {
			$this->set_error(23);
		}
		
		if($objReader->canRead($_FILES[$this->name_dateiupload_feld]['tmp_name'])) {

			$objPHPExcelFromCSV = $objReader->load($_FILES[$this->name_dateiupload_feld]['tmp_name']);
			$objWorksheet = $objPHPExcelFromCSV->getActiveSheet();

			$file = $objWorksheet->toArray();

Developer
Aug 21, 2009 at 4:35 PM

Yes, it must be an encoding problem. PHPExcel assumes UTF-8 (without BOM). Your CSV file is probably encoded in ISO-8859-1.

What you need to do is to convert the file to UTF-8 (without BOM) before loading it into PHPExcel.

PHPExcel_Reader_CSV really needs an option for setting input encoding of the CSV file. I can see that. Will create a work item for that.

 

Aug 21, 2009 at 4:44 PM

ah okay i see!

My english is not the best, so when i understand you right, you will code a function for that problem, or?

 

But another question:

When i do an excel import i get the following array back:

  1 => 
array
0 => <small>string</small> 'xxxxxxx ' (length=50)
1 => <small>string</small> 'xxxxxx ' (length=25)
2 => <small>string</small> '[$-198070]General' (length=17) <---- whats this??? the value 198070 is right but what is [$-xxxxx]General
3 => <small>string</small> '05-13-09' (length=8)
4 => <small>string</small> 'Rechtsschutz Union' (length=18)
5 => <small>string</small> '09.08.2009' (length=10)
6 => <small>string</small> '165.45' (length=6)
7 => <small>string</small> 'jährlich' (length=9) <------ Problem
8 => <small>string</small> 'MAK20917 ' (length=25)
9 => <small>string</small> '3000' (length=4)
10 => <small>string</small> 'Angebot' (length=7)
11 => <small>int</small> 14618691



How can i solve this Problem?


Developer
Aug 21, 2009 at 5:03 PM

>> you will code a function for that problem, or?

Work item created for CSV reader input encoding:

http://phpexcel.codeplex.com/WorkItem/View.aspx?WorkItemId=10500

>> But another question:

      7 => <small>string</small> 'jährlich' (length=9)  <------ Problem 

There is no problem here! It is encoded correctly as UTF-8. If you are viewing this dump text in the browser, make sure that encoding is set to UTF-8 in your browser:

Aug 24, 2009 at 12:22 PM

A simple, but a little bit dirty workaround is to put the following code into line 193:

 

         foreach($rowData as $key => $wert) {
                $rowData[$key] = trim(iconv("ISO-8859-1", "UTF-8", $wert));
            }

greets max


@koyama: Thanks for the excellent support