timmah1 Posted August 6, 2008 Share Posted August 6, 2008 How can I have PHP read a csv file, and show just the data? I have been looking around, but everything just reads the Heading and shows that in an array. What I'm trying to do is read the csv file from exporting contacts from Outlook. Thanks in advance. Link to comment https://forums.phpfreaks.com/topic/118495-solved-read-csv-file/ Share on other sites More sharing options...
DarkWater Posted August 6, 2008 Share Posted August 6, 2008 <?php $file = "contacts.csv"; $contents = file_get_contents($file); $rows = explode('\n', $contents); foreach ($rows as $k=>$v) { if ($k == 0) { continue; } //header row, don't need it. $data = explode(',', $v); print_r($data); } ?> Just an example to get you started. Link to comment https://forums.phpfreaks.com/topic/118495-solved-read-csv-file/#findComment-610032 Share on other sites More sharing options...
timmah1 Posted August 6, 2008 Author Share Posted August 6, 2008 Shows absolutely nothing when using that Link to comment https://forums.phpfreaks.com/topic/118495-solved-read-csv-file/#findComment-610036 Share on other sites More sharing options...
DarkWater Posted August 6, 2008 Share Posted August 6, 2008 Can I see what the CSV looks like (like how it's formatted)? I don't use Outlook so I don't know how it exports contacts. Link to comment https://forums.phpfreaks.com/topic/118495-solved-read-csv-file/#findComment-610043 Share on other sites More sharing options...
thebadbad Posted August 6, 2008 Share Posted August 6, 2008 "\n" instead of '\n' on line 4, so it's treated like a new line. .. But yeah, I don't happen to have Outlook either, so an example of the CSV could be helpful. Link to comment https://forums.phpfreaks.com/topic/118495-solved-read-csv-file/#findComment-610044 Share on other sites More sharing options...
DarkWater Posted August 6, 2008 Share Posted August 6, 2008 Oh yeah, sorry about that. Forgot to press Shift and then missed it when I looked it over. >_< Good catch. Link to comment https://forums.phpfreaks.com/topic/118495-solved-read-csv-file/#findComment-610053 Share on other sites More sharing options...
timmah1 Posted August 6, 2008 Author Share Posted August 6, 2008 ok, that fixed the problem. Now, how do I get it to not show as arrary's? Just 3 headings, First Name, Last Name, E-mail Address So that it would like this: test test [email protected] right now, it look like this Array ( [0] => test [1] => test [2] => [email protected] ) Link to comment https://forums.phpfreaks.com/topic/118495-solved-read-csv-file/#findComment-610060 Share on other sites More sharing options...
thebadbad Posted August 6, 2008 Share Posted August 6, 2008 Simple. Instead of print_r()'ing it, echo the implode()'d array: <?php $file = "contacts.csv"; $contents = file_get_contents($file); $rows = explode("\n", $contents); foreach ($rows as $k=>$v) { if ($k == 0) { continue; } //header row, don't need it. $data = explode(',', $v); echo implode(' ', $data), '<br />'; } ?> Or you could simply replace the commas with spaces, inside the foreach loop. Link to comment https://forums.phpfreaks.com/topic/118495-solved-read-csv-file/#findComment-610083 Share on other sites More sharing options...
timmah1 Posted August 6, 2008 Author Share Posted August 6, 2008 Perfect!! Thank you very much Link to comment https://forums.phpfreaks.com/topic/118495-solved-read-csv-file/#findComment-610087 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.