note 85385 added to function.fgetcsv

From: Date: Wed, 27 Aug 2008 22:45:21 +0000
Subject: note 85385 added to function.fgetcsv
Groups: php.notes 
Request: Send a blank email to php-notes+get-143904@lists.php.net to get a copy of this message
Here is another function that splits the rows of a CSV file and uses a comma as its delimiter. The function also ignores commas inside of double quotations and removes any double quotes and ultimately prints out the data into a table. If you need to keep the double quotes you'll need to edit the code. The function takes an array as an argument. The array I pass into the function contains the display name and CSV file name. Enjoy! <?php /** * Function to print out data from an uploaded CSV file into a table. * * @param array $data * @return */ function printCSV ($data) { if (is_array($data)) { // open the csv file and read it $dataFile = 'documents/courses/' . $data['course_file']; // change per setup - $data['course_file'] = CSV file name $open = fopen($dataFile, 'r'); $csv = fread($open, filesize($dataFile)); print "<h2>" . $data['name'] . "</h2>"; // optional // split up the rows $csv = preg_split('/[\r\n]/', $csv); $csvData = array(); $i = 0; foreach ($csv as $line) { // splits lines with a comma but ignores a comma wrapped with double quotes $csvData[$i] = preg_split('/,(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))/', $line); $i++; } print "<table width='60%' cellpadding='0' cellspacing='0' class='course-table'>"; for ($j = 0; $j < count($csvData); $j++) { // print the first line with <th> tags if ($csvData && $j == 0) { print "<tr>"; for ($k = 0; $k < count($csvData[$j]); $k++) { // remove the double quotes if (preg_match('/\"/', $csvData[$j][$k])) { print "<th>" . trim($csvData[$j][$k], '"') . "</th>"; // edit if you wish to keep the double quotes } else { print "<th>" . $csvData[$j][$k] . "</th>"; } } print "</tr>"; // print the rest with <td> tags } else { print "<tr>"; for ($k = 0; $k < count($csvData[$j]); $k++) { // remove the double quotes if (preg_match('/\"/', $csvData[$j][$k])) { print "<td>" . trim($csvData[$j][$k], '"') . "</td>"; // edit if you wish to keep the double quotes } else { print "<td>" . $csvData[$j][$k] . "</td>"; } } print "</tr>"; } } print "</table>"; } else { return false; } } ?> ---- Server IP: 208.69.120.35 Probable Submitter: 129.24.43.244 ---- Manual Page -- http://www.php.net/manual/en/function.fgetcsv.php Edit -- https://master.php.net/note/edit/85385 Del: integrated -- https://master.php.net/note/delete/85385/integrated Del: useless -- https://master.php.net/note/delete/85385/useless Del: bad code -- https://master.php.net/note/delete/85385/bad+code Del: spam -- https://master.php.net/note/delete/85385/spam Del: non-english -- https://master.php.net/note/delete/85385/non-english Del: in docs -- https://master.php.net/note/delete/85385/in+docs Del: other reasons-- https://master.php.net/note/delete/85385 Reject -- https://master.php.net/note/reject/85385 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#143904) next »