Jump to content

[SOLVED] Pull multiple random lines from text file


rachelk

Recommended Posts

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!

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.

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);
?>

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";
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.