matthew9090 Posted April 3, 2011 Share Posted April 3, 2011 I have this code so far: $file = fopen("file.txt", "r"); while(!feof($file)) { echo "<a href=" . fgets($file). ">" . fgets($file) . "</a><br />"; } fclose($file); but if i use fgets twice (like above) it makes the link, link to the previous result Link to comment https://forums.phpfreaks.com/topic/232565-using-fgets-in-while-loop-twice/ Share on other sites More sharing options...
kenrbnsn Posted April 3, 2011 Share Posted April 3, 2011 What does the content of the file look like? Ken Link to comment https://forums.phpfreaks.com/topic/232565-using-fgets-in-while-loop-twice/#findComment-1196241 Share on other sites More sharing options...
matthew9090 Posted April 3, 2011 Author Share Posted April 3, 2011 http://website1.com http://website2.com ..... Link to comment https://forums.phpfreaks.com/topic/232565-using-fgets-in-while-loop-twice/#findComment-1196242 Share on other sites More sharing options...
kenrbnsn Posted April 3, 2011 Share Posted April 3, 2011 Try: <?php $sites = array_map('trim',file('file.txt')); //read the whole file into an array & trim the newline character from the end of each line foreach ($sites as $link) { echo "<a href='$link'>$link</a><br />"; } ?> Ken Link to comment https://forums.phpfreaks.com/topic/232565-using-fgets-in-while-loop-twice/#findComment-1196243 Share on other sites More sharing options...
matthew9090 Posted April 3, 2011 Author Share Posted April 3, 2011 thanks ken Link to comment https://forums.phpfreaks.com/topic/232565-using-fgets-in-while-loop-twice/#findComment-1196244 Share on other sites More sharing options...
dcro2 Posted April 3, 2011 Share Posted April 3, 2011 If you have PHP5, you might as well just use the FILE_IGNORE_NEW_LINES flag of file. It might not make any difference if the file is small but, I don't know, I like cleaner code . $sites = file('file.txt', FILE_IGNORE_NEW_LINES); Link to comment https://forums.phpfreaks.com/topic/232565-using-fgets-in-while-loop-twice/#findComment-1196259 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.