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. 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 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 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>"; } 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 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. 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 Link to comment https://forums.phpfreaks.com/topic/48471-solved-file/#findComment-237060 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.