Jump to content

Grabbing random variables from an external file


php_joe

Recommended Posts

Hi,

I want to write a script that will pull a random line of text from an external file (such as a txt file) and then break it into peices and insert it into the webpage.

For example:
If the file contained this;

fish;water;plankton
scorpians;desert;insects
eagles;trees;fish

Then it would grab one line at random (say, line #2) and assign the sections to $species, $habitat, and $food respectivly so that I could end up with an output like this:

[b]scorpians[/b] live in the [b]desert[/b] and eat [b]insects[/b].

I don't expect anyone to write this for me (I can wish, but I don't expect), but if you could tell me what commands I need to learn and/or where to learn about them, that would help tremendously!

Thanks!

Joe
file() function will read your text file into an array of lines

[code]$lines = file('myfile.txt');[/code]

You then get the line you want from the array and trim off the linefeed from the end of the line

[code]$wanted = 2;
$line = $lines[$wanted];
$line = trim($line);[/code]

You now need to break the line at the ";" characters to get individual items

[code]$data = explode (";" , $line);[/code]

Now you can get the fields

[code]$species = $data[0];  // first item
$habitat = $data[1];
$food  = $data[2];[/code]

and outout
[code]
echo "$species lives in the $habitat and eats $food";[/code]

Putting it all together (using list() to put array items into variables in one go)

[code]$wanted = 2;
$lines = file('myfile.txt');
$line = trim($lines[$wanted]);
list ($species, $habitat, $food) = explode (";", $line);
echo "$species lives in the $habitat and eats $food";[/code]


Hope it helps
Wow!

Thank you very much for the incredably fast and detailed answer!

I figured out how to make it random too by making the following changes:

[blockquote]$lines = file('myfile.txt');
[!--coloro:#FF0000--][span style=\"color:#FF0000\"][!--/coloro--]$count = count($lines)[!--colorc--][/span][!--/colorc--];
$wanted = [!--coloro:#FF0000--][span style=\"color:#FF0000\"][!--/coloro--](rand()%$count)[!--colorc--][/span][!--/colorc--];
$line = trim($lines[$wanted]);
list ($species, $habitat, $food) = explode (";", $line);
echo "$species lives in the $habitat and eats $food";[/blockquote]

Thanks again for your help!

Joe
Now that's what I call a "snippit"!

[!--quoteo(post=375310:date=May 20 2006, 03:09 AM:name=Barand)--][div class=\'quotetop\']QUOTE(Barand @ May 20 2006, 03:09 AM) [snapback]375310[/snapback][/div][div class=\'quotemain\'][!--quotec--]
OK, minimalist version

[code]$lines = file('myfile.txt');
vprintf ('%s live in the %s and eat %s', explode(';', trim($lines[array_rand($lines)])));[/code]
[/quote]

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.