Jump to content


Photo

Grabbing random variables from an external file


  • Please log in to reply
7 replies to this topic

#1 php_joe

php_joe
  • Members
  • PipPipPip
  • Advanced Member
  • 175 posts

Posted 19 May 2006 - 10:04 AM

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:

scorpians live in the desert and eat insects.

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
[url=http://darkswordsguide.com][/url]

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 19 May 2006 - 10:55 AM

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

$lines = file('myfile.txt');

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

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

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

$data = explode (";" , $line);

Now you can get the fields

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

and outout
echo "$species lives in the $habitat and eats $food";

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

$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";


Hope it helps
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 php_joe

php_joe
  • Members
  • PipPipPip
  • Advanced Member
  • 175 posts

Posted 19 May 2006 - 03:59 PM

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

[url=http://darkswordsguide.com][/url]

#4 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 19 May 2006 - 06:10 PM

or

$count = count($lines);
$wanted = rand($count-1);

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#5 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 19 May 2006 - 06:33 PM

or
<?php $line = trim($lines[array_rand($lines)]); ?>

Ken

#6 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 19 May 2006 - 07:09 PM

OK, minimalist version

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


If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#7 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 19 May 2006 - 09:33 PM

come on barand i know you can make it down to 1 line [img src=\"style_emoticons/[#EMO_DIR#]/laugh.gif\" style=\"vertical-align:middle\" emoid=\":laugh:\" border=\"0\" alt=\"laugh.gif\" /]
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#8 php_joe

php_joe
  • Members
  • PipPipPip
  • Advanced Member
  • 175 posts

Posted 20 May 2006 - 02:45 AM

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) View Post[/div][div class=\'quotemain\'][!--quotec--]
OK, minimalist version

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

[url=http://darkswordsguide.com][/url]




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users