Skip to content

emka.web.id

Menu
  • Home
  • Indeks Artikel
  • Tutorial
  • Tentang Kami
Menu

PHP Cookbook: Parsing Comma-Separated Data

Posted on March 08, 2012 by Syauqi Wiryahasana
Problem You have data in comma-separated values (CSV) format—for example, a file exported from Excel or a database—and you want to extract the records and fields into a format you can manipulate in PHP. Solution If the CSV data is in a file (or available via a URL), open the file with fopen( ) and read in the data with fgetcsv( ) . Example 1-31 prints out CSV data in an HTML table. Example 1-31. Reading CSV data from a file [sourcecode language="php"] <?php $fp = fopen('sample2.csv','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"); ?> [/sourcecode] Discussion In PHP 4, you must provide 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.) In PHP 5 the line length is optional. Without it, fgetcsv( ) reads in an entire line of data. (Or, in PHP 5.0.4 and later, you can pass a line length of 0 to do the same thing.) 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. 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, able to 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. See Also Documentation on fgetcsv( ) at http://www.php.net/fgetcsv .
Seedbacklink

Recent Posts

TENTANG EMKA.WEB>ID

EMKA.WEB.ID adalah blog seputar teknologi informasi, edukasi dan ke-NU-an yang hadir sejak tahun 2011. Kontak: kontak@emka.web.id.

©2024 emka.web.id Proudly powered by wpStatically