Aufgabe 2: serverseitiges Parsen einer CSV-Datei fertig
This commit is contained in:
parent
d4639fe5e8
commit
15a51852f7
3 changed files with 38 additions and 7 deletions
|
@ -1,3 +1,6 @@
|
|||
<?php
|
||||
error_reporting(E_ALL);
|
||||
?>
|
||||
<!DOCTYPE html>
|
||||
<html lang="de">
|
||||
|
||||
|
|
|
@ -1,4 +1,16 @@
|
|||
<?php
|
||||
echo "Ich bin die Parse-Datei!";
|
||||
require 'world_data_parser.php';
|
||||
|
||||
$path = 'resources/world_data_v1.csv';
|
||||
|
||||
$parser = new WorldDataParser();
|
||||
|
||||
$res = $parser->parseCSV($path);
|
||||
|
||||
?>
|
||||
<pre>
|
||||
<?php
|
||||
print_r($res);
|
||||
?>
|
||||
</pre>
|
||||
|
||||
|
|
|
@ -1,7 +1,23 @@
|
|||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: Selin
|
||||
* Date: 10.11.2018
|
||||
* Time: 15:59
|
||||
*/
|
||||
class WorldDataParser {
|
||||
|
||||
function parseCSV($path) {
|
||||
$res = [];
|
||||
|
||||
if (!is_file($path)) {
|
||||
echo 'Datei nicht vorhanden! - ' . $path;
|
||||
}
|
||||
|
||||
$handle = fopen($path, "r");
|
||||
if ($handle !== FALSE) {
|
||||
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
|
||||
$res[] = $data; // data array (aktueller Zeile) zu multidimensionalen array hinzufügen
|
||||
}
|
||||
fclose($handle);
|
||||
} else {
|
||||
echo 'Konnte Datei nicht öffnen! - ' . $path;
|
||||
}
|
||||
|
||||
return $res;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue