OK I am very new to PHP. I am trying to read some values from comma-seperated File. The File has 9 Fields seperated by commas then it moves to another line. I only need the values in fields 1,3,6. I have been testing this for hours and cannot figure it out. I don't know if there is a function in php to read a value of a single line, But that what help me out. This works so far the only thing is with the code I have now for some reason gives me the values for 1,3,6, and 9. Also the file is so big the execution time exceeds the maxlimit of 30 seconds. Someone please help.... here is the code
$File = file_get_contents("C:\\Zip Codes\\premium.csv");
$Length = strlen($File);
$Count = 0;
$ComPos = 0;
$FieldPos = 1;
while ($Count <> $Length){
if ($FieldPos == 9){
$FieldPos = 1;
} // endif
If (substr($File,$Count,1) == ","){
if ($ComPos == 0){
$Record = substr($File,0,$Count);
$FieldPos = $FieldPos + 1;
}else{
if($FieldPos == 1){
$Record = $Record ." " .substr($File,$ComPos + 1, $Count - $ComPos - 1);
} // endif
if($FieldPos == 3){
$Record = $Record ." " .substr($File,$ComPos + 1, $Count - $ComPos - 1);
} // endif
if($FieldPos == 6){
$Record = $Record ." " .substr($File,$ComPos + 1, $Count - $ComPos - 1);
} // endif
$FieldPos = $FieldPos + 1;
} // endif
$ComPos = $Count;
} //endif
$Count = $Count + 1;
if ($Count == 20000){
$Count = $Length;
}// endif
} //End while
echo $Record;
exit;
function file_get_contents($f) {
ob_start();
$retval = @readfile($f);
if (false !== $retval) { // no readfile error
$retval = ob_get_contents();
}
ob_end_clean();
return $retval;
}
?>