kalm1234 Posted January 22, 2009 Share Posted January 22, 2009 I have been looking everywhere but have not found anything. I need to know how to assign an entire line, of another file, such as line 7 of the file test.txt to a variable in the text.php file. File 1 test.php <?php $myfile = 'test.txt'; $fh = fopen($myfile, 'r'); ... ?> File 2 test.txt text1 text2 text3 text4 text5 text6 text7 text8 Thanks for any help guys... Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted January 22, 2009 Share Posted January 22, 2009 Try this: $myfile = 'test.txt'; $fh = fopen($myfile, 'r'); $contents = fread($fh,filesize($myfile)); $lines = explode("\n",$contents); echo $lines[6]; // starts with 0 so use 6 to get line 7 Quote Link to comment Share on other sites More sharing options...
kalm1234 Posted January 22, 2009 Author Share Posted January 22, 2009 Thanks for the fast reply. Worked like a charm. Thanks a lot!!! Quote Link to comment Share on other sites More sharing options...
DarkWater Posted January 22, 2009 Share Posted January 22, 2009 If you're going to be loading the whole file into memory anyway, you might as well shorten that up: <?php $filename = 'test.txt'; $data = file($filename); echo $data[6]; ?> Quote Link to comment 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.