Jump to content

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]
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.