greenba Posted November 22, 2008 Share Posted November 22, 2008 Hello, I have the following in a file: 1 2008-10-28 16:20:01 1 0 1 0 2 2008-10-28 16:20:16 1 0 1 0 1 2008-10-29 06:37:09 1 0 1 0 11 2008-10-29 07:06:58 1 0 1 0 44 2008-10-29 07:07:12 1 0 1 0 2 2008-10-29 07:10:06 1 0 1 0 3 2008-10-29 13:30:05 1 1 1 0 15 2008-10-29 13:30:10 1 1 1 0 24 2008-10-29 13:30:14 1 1 1 0 3 2008-10-29 13:30:18 1 1 1 0 15 2008-10-29 13:30:23 1 1 1 0 I need to read it line by line and separate each row into 6 variables, so that for the first row; $var1 = 1 $var2 = 2008-10-28 16:20:01 $var3 = 1 $var4 = 0 $var5 = 1 $var6 = 0 and so on. I have attached a file on how it exactly looks like. Any ideas on how to do this? All help is appreciated [attachment deleted by admin] Link to comment https://forums.phpfreaks.com/topic/133775-read-file/ Share on other sites More sharing options...
Mchl Posted November 22, 2008 Share Posted November 22, 2008 fgets will let you read the file row by row explode will let you split each row into an array. Link to comment https://forums.phpfreaks.com/topic/133775-read-file/#findComment-696184 Share on other sites More sharing options...
.josh Posted November 22, 2008 Share Posted November 22, 2008 I prefer file it puts each line into an array. Keep in mind though when you explode, the date and time will be separate elements since there is a space between them. To get your $var2 you're going to have to glue those 2 elements back together. Simple string concat will do the trick. Link to comment https://forums.phpfreaks.com/topic/133775-read-file/#findComment-696188 Share on other sites More sharing options...
kenrbnsn Posted November 22, 2008 Share Posted November 22, 2008 Use fgetcsv using the space for a delimiter. You would then have to combine the date & time into one variable. Ken Link to comment https://forums.phpfreaks.com/topic/133775-read-file/#findComment-696189 Share on other sites More sharing options...
PFMaBiSmAd Posted November 22, 2008 Share Posted November 22, 2008 Or since the file is tab delimited - <?php $file = "sample.txt"; $lines = file($file); foreach($lines as $line){ $data = explode("\t",$line); $data = array_map('trim', $data); // trim any extra tabs/newlines echo "<pre>",print_r($data,true),"</pre>"; // here is what your data for each line looks like // put your code here to process the data in each line } ?> Link to comment https://forums.phpfreaks.com/topic/133775-read-file/#findComment-696194 Share on other sites More sharing options...
kenrbnsn Posted November 22, 2008 Share Posted November 22, 2008 I would still use fgetcsv: <?php $fp = fopen('sample.txt','r'); $var = array(); while (($data = fgetcsv($fp, 1000, "\t")) !== FALSE) { echo '<pre>' . print_r($data,true) . '</pre>'; } fclose($fp); ?> Ken Link to comment https://forums.phpfreaks.com/topic/133775-read-file/#findComment-696204 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.