1) There are probably better functions than fread() to read a CSV file. Checkout: fgetcsv() (which I've not used) or file() which reads the entire file into an array, one line per array element.
2) If you want to parse the CSV yourself, then be aware that explode() will not work properly if any string contains a comma. i.e. this line will return 3 elements: (some spaces added for clarity)
Code: Select all
explode(',' , ' "a","Hello World","c" ');
Code: Select all
explode(',' , ' "a","Hello,World","c" ');
Code: Select all
<?php
$aFileContents = file("SP.csv"); // Read file into an array, one element per line
foreach ($aFileContents as $sLine) { // Get elements (lines), one at a time, into $sLine
$delimiter = ",";
$myArray = explode($delimiter, $sLine);
echo $myArray[4]; // Print out the 5th element (go Milla!)
}
?>
-A