rachelk Posted September 12, 2009 Share Posted September 12, 2009 I've searched around and found threads on doing this, but I'm a beginner so I can't figure out how to do it with more than one line and not have a line repeated anywhere... I have a text file set up like words-here and-here etc and just want to pull 10 random lines from it. Any help is much appreciated! Link to comment https://forums.phpfreaks.com/topic/173981-solved-pull-multiple-random-lines-from-text-file/ Share on other sites More sharing options...
ram4nd Posted September 12, 2009 Share Posted September 12, 2009 put your text in array and pull the lines up randomly. Link to comment https://forums.phpfreaks.com/topic/173981-solved-pull-multiple-random-lines-from-text-file/#findComment-917102 Share on other sites More sharing options...
rachelk Posted September 12, 2009 Author Share Posted September 12, 2009 Thank you for your reply. I've tried many codes pulling random lines from arrays and they all end up with some lines being repeated, so I'm wondering if there's a way to avoid that. Link to comment https://forums.phpfreaks.com/topic/173981-solved-pull-multiple-random-lines-from-text-file/#findComment-917105 Share on other sites More sharing options...
thebadbad Posted September 12, 2009 Share Posted September 12, 2009 <?php $lines = file('filename.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); shuffle($lines); $ten = array_slice($lines, 0, 10); echo '<pre>' . print_r($ten, true) . '</pre>'; ?> Link to comment https://forums.phpfreaks.com/topic/173981-solved-pull-multiple-random-lines-from-text-file/#findComment-917108 Share on other sites More sharing options...
rachelk Posted September 12, 2009 Author Share Posted September 12, 2009 Thanks for that, it works perfectly with outputting 10 lines, but I can't figure out how to use it in my situation. Each line in that text file is an image url, so I'm looking to have < img src = " print line here " > for each of the ten lines instead of the lines outputting in a list. I've googled quite a bit but can't figure out how I can do this, or if it's even possible.. every code I try just outputs a list. Link to comment https://forums.phpfreaks.com/topic/173981-solved-pull-multiple-random-lines-from-text-file/#findComment-917129 Share on other sites More sharing options...
MatthewJ Posted September 12, 2009 Share Posted September 12, 2009 Borrowing from thebadbad All you would need is these two lines of code... then each place there is an image url, replace it with echo $lines[0], echo $lines[1], echo $lines[2], and so on. Sounds too simple, maybe I'm misunderstanding <?php $lines = file('filename.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); shuffle($lines); ?> Link to comment https://forums.phpfreaks.com/topic/173981-solved-pull-multiple-random-lines-from-text-file/#findComment-917150 Share on other sites More sharing options...
thebadbad Posted September 14, 2009 Share Posted September 14, 2009 Thanks for that, it works perfectly with outputting 10 lines, but I can't figure out how to use it in my situation. Each line in that text file is an image url, so I'm looking to have < img src = " print line here " > for each of the ten lines instead of the lines outputting in a list. I've googled quite a bit but can't figure out how I can do this, or if it's even possible.. every code I try just outputs a list. Just loop through the array: <?php foreach ($ten as $url) { echo '<img src="' . $url . '" alt="" />' . "\n"; } ?> Link to comment https://forums.phpfreaks.com/topic/173981-solved-pull-multiple-random-lines-from-text-file/#findComment-918212 Share on other sites More sharing options...
rachelk Posted September 15, 2009 Author Share Posted September 15, 2009 Thanks for that MatthewJ and thebadbad Link to comment https://forums.phpfreaks.com/topic/173981-solved-pull-multiple-random-lines-from-text-file/#findComment-918684 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.