johnnycashpoint Posted August 27, 2009 Share Posted August 27, 2009 Hey PHPFreaks. I am a new guy. I need the assistance your superpowers please? Here's the idea.. A user inputs a name into a form, which then gets posted. They then get a random sentence returned, containing the name they posted. To do this, I want to use a text file containing the sentences. (Note: the variable appears in different places) Eg: (example.txt) One day I went to the park with $name One day $name went to the park with me I saw $name at the park one day $name and I were at the park So, what would the code be to echo a single line at random and the variable $name to be recognized in the echo ? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/172109-echoing-mixture-of-variables-and-text-from-a-text-file/ Share on other sites More sharing options...
Adam Posted August 27, 2009 Share Posted August 27, 2009 Perhaps something like: $str = 'One day I went to the park with {name} One day {name} went to the park with me I saw {name} at the park one day {name} and I were at the park'; $str = str_replace('{name}', $name, $str); Quote Link to comment https://forums.phpfreaks.com/topic/172109-echoing-mixture-of-variables-and-text-from-a-text-file/#findComment-907438 Share on other sites More sharing options...
ignace Posted August 27, 2009 Share Posted August 27, 2009 In your text file write: One day I went to the park with %s One day %s went to the park with me I saw %s at the park one day %s and I were at the park In your php file write: $sentences = file('path/to/text/file'); $rand = array_rand($sentences); printf($sentences[$rand], $name); Quote Link to comment https://forums.phpfreaks.com/topic/172109-echoing-mixture-of-variables-and-text-from-a-text-file/#findComment-907441 Share on other sites More sharing options...
Adam Posted August 27, 2009 Share Posted August 27, 2009 Hah brain totally isn't in gear yet, read that completely wrong. Quote Link to comment https://forums.phpfreaks.com/topic/172109-echoing-mixture-of-variables-and-text-from-a-text-file/#findComment-907445 Share on other sites More sharing options...
ignace Posted August 27, 2009 Share Posted August 27, 2009 Hah brain totally isn't in gear yet, read that completely wrong. Where is this server actually located? I'm from Belgium it's 1 PM and the server's time is 7 AM Quote Link to comment https://forums.phpfreaks.com/topic/172109-echoing-mixture-of-variables-and-text-from-a-text-file/#findComment-907448 Share on other sites More sharing options...
Adam Posted August 27, 2009 Share Posted August 27, 2009 Florida I think. Mid-day here in the UK though but I'm battling a hangover. Quote Link to comment https://forums.phpfreaks.com/topic/172109-echoing-mixture-of-variables-and-text-from-a-text-file/#findComment-907452 Share on other sites More sharing options...
ignace Posted August 27, 2009 Share Posted August 27, 2009 Florida I think. Mid-day here in the UK though but I'm battling a hangover. I hope your winning Quote Link to comment https://forums.phpfreaks.com/topic/172109-echoing-mixture-of-variables-and-text-from-a-text-file/#findComment-907459 Share on other sites More sharing options...
trq Posted August 27, 2009 Share Posted August 27, 2009 Hehe, 9:30 PM here and I'm working pretty hard on a hangover ready for casual Friday at work. I do nothing but write specs/docs on Fridays, too easy. Quote Link to comment https://forums.phpfreaks.com/topic/172109-echoing-mixture-of-variables-and-text-from-a-text-file/#findComment-907462 Share on other sites More sharing options...
johnnycashpoint Posted August 27, 2009 Author Share Posted August 27, 2009 In your text file write: One day I went to the park with %s One day %s went to the park with me I saw %s at the park one day %s and I were at the park In your php file write: $sentences = file('path/to/text/file'); $rand = array_rand($sentences); printf($sentences[$rand], $name); That's great! The variable gets echoed! The only problem is, it is echoing all the lines at the same time, instead of plucking out just 1 at random Quote Link to comment https://forums.phpfreaks.com/topic/172109-echoing-mixture-of-variables-and-text-from-a-text-file/#findComment-907516 Share on other sites More sharing options...
johnnycashpoint Posted August 27, 2009 Author Share Posted August 27, 2009 I got it!! Here's how I did it. { for($i=9; $i>=0; $i=$i-1) { $linefile = "phptest.txt"; $handle = fopen($linefile, "r"); $contents = fread($handle, filesize($linefile)); fclose($handle); $word1 = explode("\r", $contents); $line = $word1[rand(0, count($word1)-1)]; echo "<p class='res'>"; printf($line, $word); echo "</p><br />"; } } Quote Link to comment https://forums.phpfreaks.com/topic/172109-echoing-mixture-of-variables-and-text-from-a-text-file/#findComment-907546 Share on other sites More sharing options...
ignace Posted August 27, 2009 Share Posted August 27, 2009 The only problem is, it is echoing all the lines at the same time, instead of plucking out just 1 at random That's not possible because i randomly select one line (as file reads a file into an array) store it in $rand and retrieve that row by using $sentences[$rand] If it would however select all rows then you would get an error from printf saying something like: too few arguments. Quote Link to comment https://forums.phpfreaks.com/topic/172109-echoing-mixture-of-variables-and-text-from-a-text-file/#findComment-907576 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.