stylus277 Posted July 2, 2008 Share Posted July 2, 2008 I have a flatfile named PROBINFO.cap. There isn't really a delimiter (not CSV...) each line is a record. I would like to set a VAR for each line and autonumber till it is done. So far I can get it to read the file and echo it back on to the screen. But I cannot set the variables correctly. <?php $fp = fopen('PROBINFO.cap','r'); if (!$fp) {echo 'ERROR: Unable to open file.'; exit;} while (!feof($fp)) { $line = fgets($fp, 1024); list ($field1) = split ('\|', $line); echo ' '.$field1.'<br>'; $fp++; } fclose($fp); ?> [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/112947-help-parsing-flatfile/ Share on other sites More sharing options...
trq Posted July 2, 2008 Share Posted July 2, 2008 Your question is pretty unclear, especially after viewing your file. Maybe? <?php $lines = file('PROBINFO.cap'); $i = 1; foreach ($lines as $line) { echo "$i $line\n"; $i++ } ?> Quote Link to comment https://forums.phpfreaks.com/topic/112947-help-parsing-flatfile/#findComment-580205 Share on other sites More sharing options...
stylus277 Posted July 2, 2008 Author Share Posted July 2, 2008 thanks for the reply, I did not get too far with it though. To try and clearify my question, how would I open PROBINFO.cap and use each individual line as a value to set unique variables? Set: $Var1 = ($line1) $Var2 = ($line2) $Var3 = ($line3) $Var4 = ($line4) Thanks Quote Link to comment https://forums.phpfreaks.com/topic/112947-help-parsing-flatfile/#findComment-580249 Share on other sites More sharing options...
trq Posted July 2, 2008 Share Posted July 2, 2008 You would be better simply storing each line in an array IMO, its more readble. Anyway, this will do what you want. <?php $lines = file('PROBINFO.cap'); $i = 1; foreach ($lines as $line) { ${'Var'.$i} = $line; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/112947-help-parsing-flatfile/#findComment-580252 Share on other sites More sharing options...
wildteen88 Posted July 2, 2008 Share Posted July 2, 2008 It is better to use an array, rather than unique variables. Just use: $lines = file('PROBINFO.cap'); $lines holds an array of lines from the propinfo file, eg $line[0] is the first line, $line[1] is the second etc. Quote Link to comment https://forums.phpfreaks.com/topic/112947-help-parsing-flatfile/#findComment-580254 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.