All pastes #41689 Raw Edit

CSV

public php v1 · immutable
#41689 ·published 2006-02-15 02:55 UTC
rendered paste body
<?php$csvFile=<<<CSV"Subject","Start Date","Start Time","End ""End of days, The"" Date","End Time","All day event","Description","Show time as","Location""1st of Tevet 5766","1/1/2006",,,,"true","","3","Jewish Holidays""Rosh Chodesh Tevet","1/1/2006",,,,"true","","3","Jewish Holidays""Chanukah: 8 Candles","1/1/2006",,,,"true","","3","Jewish Holidays""2nd of Tevet 5766","1/2/2006",,,,"true","","3","Jewish Holidays""Chanukah: 8th Day","1/2/2006",,,,"true","","3","Jewish Holidays""3rd of Tevet 5766","1/3/2006",,,,"true","","3","Jewish Holidays""4th of Tevet 5766","1/4/2006",,,,"true","","3","Jewish Holidays""5th of Tevet 5766","1/5/2006",,,,"true","","3","Jewish Holidays""6th of Tevet 5766","1/6/2006",,,,"true","","3","Jewish Holidays""7th of Tevet 5766","1/7/2006",,,,"true","","3","Jewish Holidays""Parshas Vayigash","1/7/2006",,,,"true","","3","Jewish Holidays""8th of Tevet 5766","1/8/2006",,,,"true","","3","Jewish Holidays""9th of Tevet 5766","1/9/2006",,,,"true","","3","Jewish Holidays""10th of Tevet 5766","1/10/2006",,,,"true","","3","Jewish Holidays""Asara B'Tevet","1/10/2006",,,,"true","","3","Jewish Holidays""11th of Tevet 5766","1/11/2006",,,,"true","","3","Jewish Holidays""12th of Tevet 5766","1/12/2006",,,,"true","","3","Jewish Holidays""13th of Tevet 5766","1/13/2006",,,,"true","","3","Jewish Holidays""14th of Tevet 5766","1/14/2006",,,,"true","","3","Jewish Holidays""Parshas Vayechi","1/14/2006",,,,"true","","3","Jewish Holidays""15th of Tevet 5766","1/15/2006",,,,"true","","3","Jewish, ""happy, happy, joy, joy"", Holidays""16th of Tevet 5766","1/16/2006",,,,"true","","3","Jewish Holidays""17th of Tevet 5766","1/17/2006",,,,"true","","3","Jewish Holidays""18th of Tevet 5766","1/18/2006",,,,"true","","3","Jewish Holidays""19th of Tevet 5766","1/19/2006",,,,"true","","3","Jewish Holidays""20th of Tevet 5766","1/20/2006",,,,"true","","3","Jewish Holidays""21st of Tevet 5766","1/21/2006",,,,"true","","3","Jewish Holidays""Parshas Shemos","1/21/2006",,,,"true","","3","Jewish Holidays""22nd of Tevet 5766","1/22/2006",,,,"true","","3","Jewish Holidays""23rd of Tevet 5766","1/23/2006",,,,"true","","3","Jewish Holidays""24th of Tevet 5766","1/24/2006",,,,"true","","3","Jewish Holidays""25th of Tevet 5766","1/25/2006",,,,"true","","3","Jewish Holidays""26th of Tevet 5766","1/26/2006",,,,"true","","3","Jewish Holidays""27th of Tevet 5766","1/27/2006",,,,"true","","3","Jewish Holidays""28th of Tevet 5766","1/28/2006",,,,"true","","3","Jewish Holidays""Parshas Vaera","1/28/2006",,,,"true","","3","Jewish Holidays""29th of Tevet 5766","1/29/2006",,,,"true","","3","Jewish Holidays""1st of Sh'vat 5766","1/30/2006",,,,"true","","3","Jewish Holidays""Rosh Chodesh Sh'vat","1/30/2006",,,,"true","","3","Jewish Holidays""2nd of Sh'vat 5766","1/31/2006",,,,"true","","3","Jewish Holidays"CSV;echo "<PRE>";$c = new csv();$c->openFile($csvFile);$c->_parseHeader();$c->_parseRows();print_r($c->header);print_r($c->rows);echo "</PRE>";class csv{	var $header;	var $rows;	var $_rows;	var $_myFile;	var $_separator;	var $_textDelimeter;		function csv(){		//constructor	}		function openFile($file,$separator=',',$textDelimeter='"'){		// will be replaced for a proper fopen later		$this->_myFile = $file;		$this->_separator=$separator;		$this->_textDelimeter=$textDelimeter;		$tmpLines=explode("\n",$this->_myFile);		$counter=0;		$this->_rows=array();		foreach ($tmpLines as $line) {			if ($counter<1){				$this->header=$line;				$counter++;			}else{				$this->_rows[]=$line;			}		}		unset($counter);		unset($tmpLines);		unset($line);	}		function _parseHeader($line=""){		// first we split the line with the separator		if ($line==""){			$line=$this->header;		}		$this->header = $this->_csvExplode($line);		unset($line);	}	function _parseRows(){		$this->rows=array();		foreach ($this->_rows as $row){			$this->rows[]=$this->_csvExplode($row);		}	}		function _csvExplode($line){		// run through each character... if we are inside a "' we dont explode else, explode		$open = false;		$exploded=array();		$currentCol="";		for ($i=0;$i<strlen($line);$i++){			$c = substr($line,$i,1);			if ($c==$this->_textDelimeter){				$open=($open?false:true);			}			if ($c==$this->_separator){				if ($open){					$currentCol.=$c;				}else{					$exploded[]=ltrim(rtrim($currentCol,$this->_textDelimeter),$this->_textDelimeter);					$currentCol="";				}			}			if ($c!=$this->_separator){				$currentCol.=$c;			}			if ($i==(strlen($line)-1)){				$exploded[]=ltrim(rtrim($currentCol,$this->_textDelimeter),$this->_textDelimeter);				$currentCol="";			}		}		return $exploded;	}}?>