PHP Strings
Parsing Comma-Separated Data
Problem
You have data in comma-separated values (CSV) format—for example, a file exportedfrom Excel or a database—and you want to extract the records and fields into a format
you can manipulate in PHP.
Solution
Example Reading CSV data from a file
$fp = fopen($filename,'r') or die("can't open file");
print "<table>\n";
while($csv_line = fgetcsv($fp)) {
print '<tr>';
for ($i = 0, $j = count($csv_line); $i < $j; $i++) {
print '<td>'.htmlentities($csv_line[$i]).'</td>';
}
print "</tr>\n";
}
print "</table>\n";
fclose($fp) or die("can't close file");
Discussion
By default, fgetcsv() reads in an entire line of data. If your average line length is more
than 8,192 bytes, your program may run faster if you specify an explicit line length
instead of letting PHP figure it out. Do this by providing a second argument to
fgetcsv() that is a value larger than the maximum length of a line in your CSV file.
(Don’t forget to count the end-of-line whitespace.) If you pass a line length of 0, PHP
will use the default behavior.
You can pass fgetcsv() an optional third argument, a delimiter to use instead of a
comma (,). However, using a different delimiter somewhat defeats the purpose of CSV
as an easy way to exchange tabular data.
Don’t be tempted to bypass fgetcsv() and just read a line in and explode() on the
commas. CSV is more complicated than that so that it can deal with field values that
have, for example, literal commas in them that should not be treated as field delimiters.
Using fgetcsv() protects you and your code from subtle errors.
No comments:
Post a Comment