Location: PHPKode > projects > FxFramework > fxFrm/fxCsvParser.php
<?php

include_once('fxErrors.php');

class FxCsvParser
{
	public static function fxCsvParse($csvString, $csvSeparator, $makeAssoc, $header = "")
	{
		//Try to splip by CR/LF CR LF
		$arrData = split($csvSeparator."[\n\r\]", $csvString);
		$retAr = array();
		
		if(!is_array($arrData))
		{
			FxError::fxCritical("The Csv String passed to the parser is invalid.");
		}
		
		//Parse header. 
		//If none is provided first line of cvs is used as header later on
		$arHeader = (($header != "") && ($makeAssoc)) ? split($csvSeparator, $header) : "";
		if(($makeAssoc) && ($header != "") && (!is_array($arHeader)))
		{
			FxError::fxCritical("Bad header provided for CSV parser.");
		}
		
		$index = 0;
		foreach ($arrData as $line)
		{
			if(($makeAssoc) && (!is_array($arHeader)))
			{
				$arHeader = split($csvSeparator, $line);
				if(!is_array($arHeader))
				{
					FxError::fxCritical("The Csv String passed to the parser is invalid.");
				}
				//var_dump( $arHeader);
			}
			else 
			{	
				if(trim($line) != "")
				{
					$arLine = split($csvSeparator, $line);
					if(!is_array($arLine))
					{
						FxError::fxCritical("The Csv String passed to the parser is invalid.");
					}
					if($makeAssoc)
					{
						foreach($arLine as $key => $value)
						{
							$retAr[$index][$arHeader[$key]] = str_replace("\"", "", $value);
						}
					}
					else 
					{
						$retAr[$index] = $arLine;
					}
					$index++;
				}
			}
		}
		
		return $retAr;
		
	}
}

?>
Return current item: FxFramework