Moin,
Ich habe mir eine Kleine Klasse geschrieben und ein Upload script:
wenn ich jetzt bei beim DropDown ; auswähle macht er es einfach nicht, die anderen beiden werden aber Ohne murren übernommen...
ONLINE DEMO:
http://dev.gunah.org/csv/insert.php
Gruß
Gunah
Ich habe mir eine Kleine Klasse geschrieben und ein Upload script:
PHP:
<?php
/*
$csv = NEW csvimport('test.csv');
$csv->csvspacer(',');
$csv->set_charset('windows-1252','iso-8859-15');
$data = $csv->display();
print_r( $data );
*/
class csvimport {
var $charset_in = 'iso-8859-1';
var $charset_out = 'iso-8859-15';
var $symbol = ",";
/*
filename
*/
function csvimport($filename) {
$this->filename = $filename;
$this->readfirstline();
}
/*
csv spacer symbol
*/
function csvspacer($symbol) {
$this->symbol = $symbol;
}
/*
set in- and output charset
*/
function set_charset($input='iso-8859-1',$output='iso-8859-15') {
$this->charset_in = $input;
$this->charset_out = $output;
}
/*
charset converter
*/
function convert ($str) {
return iconv($this->charset_in,$this->charset_out,$str);
}
/*
create better names for array
*/
function array_name ($str) {
$str = strtolower($str);
$search = array(
' ', //0
'ä', //1
'Ä', //2
'ü', //3
'Ü', //4
'ö', //5
'Ö', //6
'ß', //7
"\n", //8
"\r" //9
);
$replace = array(
'_', //
'ae', //1
'ae', //2
'ue', //3
'ue', //4
'oe', //5
'oe', //6
'ss', //7
'_', //8
'' //9
);
$str = str_replace($search, $replace, $str);
$str = str_replace('__','_',$str);
$str = $this->chars($str);
return $str;
}
/*
EUR currency design to US Format
*/
function currency2plain ( $str ) {
$str = preg_replace("/[^0-9.,]/",'',$str);
$str = str_replace('.','',$str);
$str = str_replace(',','.',$str);
$str = trim($str);
return $str;
}
/*
replace all non nummeric
*/
function int ( $int ) {
return preg_replace("/[^0-9]/",'',$int);
}
/*
normal chars
*/
function chars ( $nr ) {
return preg_replace("/[^a-zA-Z0-9._\-]/",'',$nr);
}
/*
get firstline from csv
*/
function readfirstline() {
$this->handle = fopen ($this->filename,"r");
$this->csvdata = fgetcsv ($this->handle, 1000, $this->symbol);
$this->close();
}
/*
display csv file as array
*/
function display() {
$row = '0';
$csv = '';
$this->handle = fopen ($this->filename,"r");
while ( ($data = fgetcsv ($this->handle, 1000, $this->symbol)) !== FALSE ) {
$row++;
if($row!='1') {
$i = '';
foreach($data as $data) {
$i++;
$csv[$row-2][$this->array_name($this->csvdata[$i-1])] = $this->convert($data);
}
}
}
$this->close();
return $csv;
}
/*
fopen close
*/
function close() {
fclose ($this->handle);
}
/*
delete input file
*/
function rm() {
unlink($this->filename);
}
}
?>
PHP:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>TestUpload</title>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-15" />
</head>
<body>
<?php
require_once('class_csv.inc.php');
if(!defined('SID')) {
define('SID','123456789');
}
echo '<form method="post" action="'.$_SERVER['PHP_SELF'].'" enctype="multipart/form-data">
CSV Datei
<input type="file" name="csv" />
<br />
Trennzeichen
<select name="char">
<option value="comma">Komma [,] (CSV Standard)</option>
<option value="semicolon">Semikolon [;] (Excel-Standard)</option>
<option value="tab">Tab [\t]</option>
</select>
<br />
Zeichenkoderiung:
<select name="charset_in">
<option value="windows-1252">WinLatin1 (Excel-Standard)</option>
<option value="latin1">ISO-8859-1 (latin1)</option>
<option value="latin15">ISO-8859-15 (latin15)</option>
<option value="utf-8">UTF-8 (Unicode)</option>
</select>
<br />
<input type="hidden" name="XTCsid" value="'.SID.'" />
<input type="submit" value="einlesen" />
</form>';
if($_POST) {
switch($_POST['char']) {
case 'semicolon':
$_POST['char'] = ";";
break;
case 'tab':
$_POST['char'] = "\t";
break;
default:
$_POST['char'] = ",";
}
switch($_POST['charset_in']) {
case 'windows-1252':
$_POST['charset_in'] = 'windows-1252';
break;
case 'latin1':
$_POST['charset_in'] = 'latin1';
break;
case 'latin15':
$_POST['charset_in'] = "latin15";
break;
default:
$_POST['charset_in'] = "utf-8";
}
$csv = NEW csvimport($_FILES['csv']['tmp_name']);
$csv->csvspacer($_POST['char']);
$csv->set_charset($_POST['charset_in'],'iso-8859-15');
$data = $csv->display();
$csv->rm();
echo '<pre>';
print_r( $csv );
print_r( $data );
echo '</pre>';
}
?>
</body>
</html>
wenn ich jetzt bei beim DropDown ; auswähle macht er es einfach nicht, die anderen beiden werden aber Ohne murren übernommen...
ONLINE DEMO:
http://dev.gunah.org/csv/insert.php
Gruß
Gunah
Zuletzt bearbeitet: