boneXXX Posted April 24, 2007 Share Posted April 24, 2007 Could you give me an example about how to read a text file into an array and echo the each line(each array element) by using file(). Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/48471-solved-file/ Share on other sites More sharing options...
MadTechie Posted April 24, 2007 Share Posted April 24, 2007 fread Quote Link to comment https://forums.phpfreaks.com/topic/48471-solved-file/#findComment-237028 Share on other sites More sharing options...
boneXXX Posted April 24, 2007 Author Share Posted April 24, 2007 $user_file = @fopen("../datafile/member.TXT", "r"); if ($user_file) { while (!feof($user_file)) { $lines[] = fgets($user_file, 4096); } fclose($user_file); I think this what I am looking for. But shouldn't I need to put a variable inside the "$line[$n]" to print a specific line(element of array) ? Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/48471-solved-file/#findComment-237044 Share on other sites More sharing options...
clown[NOR] Posted April 24, 2007 Share Posted April 24, 2007 you can try this: $user_file = @fopen("../datafile/member.TXT", "r"); if ($user_file) { while (!feof($user_file)) { $lines[] = fgets($user_file, 4096); } fclose($user_file); foreach ($lines as $line) { echo $line."<br>"; } Quote Link to comment https://forums.phpfreaks.com/topic/48471-solved-file/#findComment-237046 Share on other sites More sharing options...
MadTechie Posted April 24, 2007 Share Posted April 24, 2007 yep try <?php $user_file = @fopen("../datafile/member.TXT", "r"); if ($user_file) { while (!feof($user_file)) { $lines[] = fgets($user_file, 4096); } fclose($user_file); foreach($lines as $line => $data) { echo ($line+1).": ".$data; } } ?> *untested LOL clown[NOR] beat me Quote Link to comment https://forums.phpfreaks.com/topic/48471-solved-file/#findComment-237047 Share on other sites More sharing options...
boneXXX Posted April 24, 2007 Author Share Posted April 24, 2007 Thanks a lot it works. My second question is, how can I print the just the 2nd line(for example). the second element of the array [1]. ***Update never mind : $user_file = @fopen("../datafile/member.TXT", "r"); if ($user_file) { while (!feof($user_file)) { $lines[] = fgets($user_file, 4096); } fclose($user_file); echo "$lines[1]"; } Thanks for the help guys. Quote Link to comment https://forums.phpfreaks.com/topic/48471-solved-file/#findComment-237053 Share on other sites More sharing options...
MadTechie Posted April 24, 2007 Share Posted April 24, 2007 cool please click solved Quote Link to comment https://forums.phpfreaks.com/topic/48471-solved-file/#findComment-237060 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.