jobo: Trim CSV Data, Refactoring Iteration replace Recursion

Beitrag lesen

Hallo,

  
<?php  
class CSV  
{  
	// Wird mit CSV-Daten bestückt  
	private $_table = NULL;  
	  
	function __construct($filePath) {  
		$fileHandle = @fopen($filePath,"r");  
		if ($fileHandle) {  
			while (($row = fgetcsv($fileHandle)) !== false) {  
				//exclude empty lines  
				if (!is_array($row)) {  
					continue;  
				}  
				foreach ($row as $cellValue) {  
					$trimmedRow[] = trim($cellValue);  
				}  
				$this->_table[] = $trimmedRow;  
			}	  
			// merci handle, go free  
			fclose($fileHandle);  
		}  
	}	  
	  
	public function getTable() {  
		if (NULL !== $this->_table) {  
			return $this->_table;  
		}  
		return false;  
	}  
}  
$csv = new CSV("teaast.csv");  
var_dump($csv->getTable());  

Filehandle nur schließen, wenn Öffnen erfolgreich war.

Gruß

jobo