Mit dem folgenden PHP-Schnipsel kann der gesamte Inhalt, so wie auch einzelne Zeilen, einer CSV-Datei ausgelesen werden. Wer Interesse hat, kann den nachfolgenden Quelltext gerne unter Berücksichtigung der GNU AGPL weiterverwenden und/oder erweitern.
<?php
/**
* This function returns the whole content of a CSV file
* or the content of the specified row as an array.
*
* @author Pascal Hollenstein <webmaster@zockerade.com>
* @version 1.0
* @license GNU AGPL
*
* @param $file_path The path to the CSV file.
* @param $row_to_return The row which has to be returned (this parameter is optional).
* @param $delimiter The delimiter that separates the values (this parameter is optional).
*
* @return array
*/
function get_csv_content($file_path, $row_to_return = "*", $delimiter = ",") {
$csv_content = array();
if (is_readable($file_path)) {
$handle = fopen($file_path, "r");
while (($current_data = fgetcsv($handle, 0, $delimiter)) !== false) {
if (count($csv_content) === $row_to_return) {
fclose($handle); return $current_data;
}
array_push($csv_content, $current_data);
}
if (is_integer($row_to_return) && !array_key_exists($row_to_return, $csv_content)) {
fclose($handle); return array();
}
fclose($handle);
}
return $csv_content;
}
?>