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... Link to comment https://forums.phpfreaks.com/topic/141871-assign-file-line-to-variable/ 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 Link to comment https://forums.phpfreaks.com/topic/141871-assign-file-line-to-variable/#findComment-742799 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!!! Link to comment https://forums.phpfreaks.com/topic/141871-assign-file-line-to-variable/#findComment-742803 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]; ?> Link to comment https://forums.phpfreaks.com/topic/141871-assign-file-line-to-variable/#findComment-742806 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.