php_joe Posted May 19, 2006 Share Posted May 19, 2006 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;planktonscorpians;desert;insectseagles;trees;fishThen 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 Quote Link to comment https://forums.phpfreaks.com/topic/9989-grabbing-random-variables-from-an-external-file/ Share on other sites More sharing options...
Barand Posted May 19, 2006 Share Posted May 19, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/9989-grabbing-random-variables-from-an-external-file/#findComment-37118 Share on other sites More sharing options...
php_joe Posted May 19, 2006 Author Share Posted May 19, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/9989-grabbing-random-variables-from-an-external-file/#findComment-37193 Share on other sites More sharing options...
Barand Posted May 19, 2006 Share Posted May 19, 2006 or[code]$count = count($lines);$wanted = rand($count-1);[/code] Quote Link to comment https://forums.phpfreaks.com/topic/9989-grabbing-random-variables-from-an-external-file/#findComment-37228 Share on other sites More sharing options...
kenrbnsn Posted May 19, 2006 Share Posted May 19, 2006 or[code]<?php $line = trim($lines[array_rand($lines)]); ?>[/code]Ken Quote Link to comment https://forums.phpfreaks.com/topic/9989-grabbing-random-variables-from-an-external-file/#findComment-37232 Share on other sites More sharing options...
Barand Posted May 19, 2006 Share Posted May 19, 2006 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 Link to comment https://forums.phpfreaks.com/topic/9989-grabbing-random-variables-from-an-external-file/#findComment-37242 Share on other sites More sharing options...
.josh Posted May 19, 2006 Share Posted May 19, 2006 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\" /] Quote Link to comment https://forums.phpfreaks.com/topic/9989-grabbing-random-variables-from-an-external-file/#findComment-37291 Share on other sites More sharing options...
php_joe Posted May 20, 2006 Author Share Posted May 20, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/9989-grabbing-random-variables-from-an-external-file/#findComment-37357 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.