DJTim666 Posted June 10, 2007 Share Posted June 10, 2007 Ok, say I made a txt file with 5000+ words in it, and I wanted to call them to be random. So when someone refreshes it will show, hello(one of the words in the file), then when they refresh again, it will say bang(another word in the file). It is going to be for a random word game that I am gunna code very shortly. It will be; 1. Someone types a word, then clicks enter. 2. It takes them to the next page where one of the words from the file are displayed. So it will look like this; You say: (whatever they type goes here). The computer says: (one of the words from the file) I know it sounds stupid, but it's gunna be cool once I figure out how to do the file thing. Any help is appreciated. -- DJ Quote Link to comment Share on other sites More sharing options...
soycharliente Posted June 10, 2007 Share Posted June 10, 2007 Off the top of my head... <?php $filename = "/usr/local/something.txt"; $handle = fopen($filename, "r"); $contents = fread($handle, filesize($filename)); fclose($handle); $words = array(explode($contents, " ")); $length = count($words); $num = rand(0, $length); return $words[$num]; ?> Did not test that. Quote Link to comment Share on other sites More sharing options...
DJTim666 Posted June 10, 2007 Author Share Posted June 10, 2007 Alright, now explain that to me like I am a 2 year old. How do I display the word xD. Quote Link to comment Share on other sites More sharing options...
soycharliente Posted June 10, 2007 Share Posted June 10, 2007 <?php function getWord() { $filename = "something.txt"; //specifies what file to use $handle = fopen($filename, "r"); //opens a connection to the file $contents = fread($handle, filesize($filename)); //from file, reads the entire filesize fclose($handle); //close connection //i assumed that your file contained only the words you wants separated by spaces $words = explode(" ", $contents); //creates a list called $words $length = count($words); //counts how many words in the list $num = rand(0, $length-1); //generates a random number from 0 to the length of the list (-1 because it's inclusive) return $words[$num]; //returns the word at the random position } ?> Random word: <?php echo getWord(); ?> So, the best way to use this would be to wrap it in a function and call the function when the page loads. This is how that looks: http://www.charlieholder.com/randomWord.php Here's the text file with the words: http://www.charlieholder.com/something.txt I had a few errors in my code. 1. explode() returns an array so you don't have to wrap it in array(). 2. explode() takes a delimiter and THEN a string, not the other way around. Quote Link to comment Share on other sites More sharing options...
DJTim666 Posted June 15, 2007 Author Share Posted June 15, 2007 . I just got back from vacation, and I tested the file out it works perfectly for what I want. Thanks Charlie ! Quote Link to comment 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.