JSHINER Posted March 13, 2008 Share Posted March 13, 2008 I have a CSV file in the following format: (It's automatically created by a program) Title;"Sub";"2008-02-01";"2008-02-01 10:23";"2008-02-01 10:45";"00:22";"" How can I get that to a readable format and display in a table? Link to comment https://forums.phpfreaks.com/topic/95964-converting-a-csv-file/ Share on other sites More sharing options...
BlueSkyIS Posted March 13, 2008 Share Posted March 13, 2008 read in the file and explode() each line using a semi-colon. that will give you an array that you can display in an HTML table. Link to comment https://forums.phpfreaks.com/topic/95964-converting-a-csv-file/#findComment-491272 Share on other sites More sharing options...
kenrbnsn Posted March 13, 2008 Share Posted March 13, 2008 Use the function fgetcsv() specifying the semi-colon as the separator. Ken Link to comment https://forums.phpfreaks.com/topic/95964-converting-a-csv-file/#findComment-491356 Share on other sites More sharing options...
JSHINER Posted March 14, 2008 Author Share Posted March 14, 2008 Here's where I'm at with this: <?php require('Database.php'); $db = new Database(); $handle = fopen('Mike.csv', 'r'); echo' <table width="100%" border="0" cellspacing="0" cellpadding="0"> '; while ($row = fgetcsv($handle)) { if($db->escape(trim($row[0]))=="Project") { echo' <tr> <td><b>Project</b></td> <td><b>Task</b></td> <td><b>Date</b></td> <td><b>Start</b></td> <td><b>End</b></td> <td><b>Duration</b></td> </tr>'; } else { echo' <tr> <td>', $db->escape(trim($row[0])), '</td> <td>', $db->escape(trim($row[1])), '</td> <td>', date("F j, Y", strtotime($db->escape(trim($row[2])))), '</td> <td>', date("g:i a", strtotime($db->escape(trim($row[3])))), '</td> <td>', date("g:i a", strtotime($db->escape(trim($row[4])))), '</td> <td>', $db->escape(trim($row[5])), '</td> </tr>'; } } echo '</table>'; fclose($handle); $db->close(); Now the only problem is I need to take the original CSV file and in Excel do Replace all for ( ; to , ) and ( " = (delete) ) I'm sure there must be a way to do this with preg_replace - but I can't get it to work. Any suggestions? Link to comment https://forums.phpfreaks.com/topic/95964-converting-a-csv-file/#findComment-492318 Share on other sites More sharing options...
accident Posted March 14, 2008 Share Posted March 14, 2008 the only problem with that is if there is a valid ; (that isnt a delimiter) in the document it will also get converted to a , use the fgetcsv() like ken stated Link to comment https://forums.phpfreaks.com/topic/95964-converting-a-csv-file/#findComment-492320 Share on other sites More sharing options...
JSHINER Posted March 14, 2008 Author Share Posted March 14, 2008 There are no other ; in the files. Link to comment https://forums.phpfreaks.com/topic/95964-converting-a-csv-file/#findComment-492321 Share on other sites More sharing options...
kenrbnsn Posted March 14, 2008 Share Posted March 14, 2008 You can specify using the ";" as the delimiter in the fgetcsv() function. By default the double quote is used to delimit strings, so you shouldn't have to remove it. Ken Link to comment https://forums.phpfreaks.com/topic/95964-converting-a-csv-file/#findComment-492322 Share on other sites More sharing options...
JSHINER Posted March 14, 2008 Author Share Posted March 14, 2008 Worked perfect. Thanks! (If there were a Solved button I'd click it) Link to comment https://forums.phpfreaks.com/topic/95964-converting-a-csv-file/#findComment-492328 Share on other sites More sharing options...
JSHINER Posted March 14, 2008 Author Share Posted March 14, 2008 Actually one last question. How can I get the contents of a directory? For example if /folder/ contained the following files: - one.csv - two.csv - three.csv How could I create a while statement to display: one.csv - two.csv - three.csv Link to comment https://forums.phpfreaks.com/topic/95964-converting-a-csv-file/#findComment-492332 Share on other sites More sharing options...
accident Posted March 14, 2008 Share Posted March 14, 2008 if ($handle = opendir('/folder/')) while (false !== ($file = readdir($handle))) echo "$file - "; Link to comment https://forums.phpfreaks.com/topic/95964-converting-a-csv-file/#findComment-492337 Share on other sites More sharing options...
kenrbnsn Posted March 14, 2008 Share Posted March 14, 2008 Use the glob() function: <?php echo implode(' - ',array_map('basename',glob('/folder/*.csv'))) . "<br>\n"; ?> Ken Link to comment https://forums.phpfreaks.com/topic/95964-converting-a-csv-file/#findComment-492349 Share on other sites More sharing options...
JSHINER Posted March 14, 2008 Author Share Posted March 14, 2008 Thanks! Link to comment https://forums.phpfreaks.com/topic/95964-converting-a-csv-file/#findComment-492352 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.