shage Posted July 5, 2007 Share Posted July 5, 2007 Anyone point me in the right direction for a way to have something like I (walked,ran,skipped,jogged) to the market The script would select one word in the () and use it. Thank you Link to comment https://forums.phpfreaks.com/topic/58542-random/ Share on other sites More sharing options...
trq Posted July 5, 2007 Share Posted July 5, 2007 <?php $words = array('walked','ran','skipped','jogged'); echo "I ". $words[rand(0,3)] . " to the market"; ?> Link to comment https://forums.phpfreaks.com/topic/58542-random/#findComment-290356 Share on other sites More sharing options...
BillyBoB Posted July 5, 2007 Share Posted July 5, 2007 lol nice thorpe this is kinda hmmmm simple Link to comment https://forums.phpfreaks.com/topic/58542-random/#findComment-290357 Share on other sites More sharing options...
shage Posted July 5, 2007 Author Share Posted July 5, 2007 thats nice but would be a pain to use if i was to write a story and change one word per paragraph, thank you thou Link to comment https://forums.phpfreaks.com/topic/58542-random/#findComment-290368 Share on other sites More sharing options...
trq Posted July 5, 2007 Share Posted July 5, 2007 You asked for an example, there it is. It would be pretty easy to build upon. Link to comment https://forums.phpfreaks.com/topic/58542-random/#findComment-290370 Share on other sites More sharing options...
shage Posted July 5, 2007 Author Share Posted July 5, 2007 k u the man Link to comment https://forums.phpfreaks.com/topic/58542-random/#findComment-290371 Share on other sites More sharing options...
BillyBoB Posted July 5, 2007 Share Posted July 5, 2007 why do you think that??? you could do like: <?php $verb = array('walked','ran','skipped','jogged'); $adj = array('orange','red','blue','yellow'); $noun = array('computer','sign','door','wall'); echo "I ". $verb[rand(0,3)] . " to the market and saw a " . $adj[rand(0,3)] . " toy car so i stole it and threw it at the " . $noun[rand(0,3)]; ?> always an interesting outcome Link to comment https://forums.phpfreaks.com/topic/58542-random/#findComment-290372 Share on other sites More sharing options...
trq Posted July 5, 2007 Share Posted July 5, 2007 And... to take that a step further. <?php function parse_replace($str) { $verb = array('walked','ran','skipped','jogged'); $adj = array('orange','red','blue','yellow'); $noun = array('computer','sign','door','wall'); $str = str_replace( array('[verb]','[adj]','[noun]'), array($verb[rand(0,count($verb)-1)],$adj[rand(0,count($adj)-1)],$noun[rand(0,count($noun)-1)]), $str ); return $str; } echo parse_replace("I [verb] to the market and saw a [adj] toy car so i stole it and threw it at the [noun]"); ?> Depends what your doing really. Link to comment https://forums.phpfreaks.com/topic/58542-random/#findComment-290387 Share on other sites More sharing options...
shage Posted July 5, 2007 Author Share Posted July 5, 2007 guys are getting there and looking good, but i cant figure out how to set it up to use the concept of (red,blue,green,orange) in the text and it random one of them, is how its done but cant seem to figure it out. Link to comment https://forums.phpfreaks.com/topic/58542-random/#findComment-290392 Share on other sites More sharing options...
trq Posted July 5, 2007 Share Posted July 5, 2007 Maybe if you explain exactly what your trying to do / post some code. Link to comment https://forums.phpfreaks.com/topic/58542-random/#findComment-290394 Share on other sites More sharing options...
per1os Posted July 5, 2007 Share Posted July 5, 2007 guys are getting there and looking good, but i cant figure out how to set it up to use the concept of (red,blue,green,orange) in the text and it random one of them, is how its done but cant seem to figure it out. You would need to use regular expressions (www.php.net/ereg) to grab all the text data in between parans, than from there filter each of those into an array and do the above code on them to get the one word, put each word in an array and use either str_replace to replace the entire (word,word) with the new word. This takes into account that the text will never have parans as part of the actual text. Shouldn't be too hard to do. Link to comment https://forums.phpfreaks.com/topic/58542-random/#findComment-290397 Share on other sites More sharing options...
BillyBoB Posted July 5, 2007 Share Posted July 5, 2007 thorpe you are genius man thats pretty great work Link to comment https://forums.phpfreaks.com/topic/58542-random/#findComment-290399 Share on other sites More sharing options...
shage Posted July 5, 2007 Author Share Posted July 5, 2007 if i had the code i wouldnt need help, lol The idea that (i,we) have is to have it pick a random word from within the (). This is to allow a story to change depending on every (load, refresh). The thing is (i,we) can't seem to figure out how to random whats inside the () as it will change from story to story. Later we hope to have it where the kids can enter their own parts of the story, showing action words etc, kind of a fun game. Link to comment https://forums.phpfreaks.com/topic/58542-random/#findComment-290402 Share on other sites More sharing options...
trq Posted July 5, 2007 Share Posted July 5, 2007 From the examples weve posted this should be easy. I'm sorry, but were not going to write it for you. If you don't understand it from here then I see little hope. Not to be harsh.... but I just can't see how we could explain things any clearer. Link to comment https://forums.phpfreaks.com/topic/58542-random/#findComment-290408 Share on other sites More sharing options...
BillyBoB Posted July 5, 2007 Share Posted July 5, 2007 ok look i will explain exactly what you should need: for the first part the () words: $words = array('word1','word2','word3'); ok now for the random part: $selection = $words[rand(0,count($words)-1)]; tada now just take $selection and use it... Link to comment https://forums.phpfreaks.com/topic/58542-random/#findComment-290412 Share on other sites More sharing options...
per1os Posted July 5, 2007 Share Posted July 5, 2007 <?php $text = "The (quick,slow) brown fox (hopped,jumped,leaped) over the (stubborn,stingy,nice) fence!"; if (eregi("\(([A-Z0-9,-_]*)\)", $text, $regs)) { // edited here to add the comma and hyphen and underscore. foreach ($regs as $match) { // unsure if the paran portion is included so lets replace them. $new_text = str_replace("\(", "", $match); $new_text = str_replace(")", "", $new_text); // this may need escaping the ( \) ) $words = explode(",", $new_text); $new_text = "(" . $new_text . ")"; // add them back for replacing later. $selection = $words[rand(0,count($words)-1)]; $text = str_replace($new_text, $selection, $text); } echo $text; } ?> That is the gist of it. I just coded it on the fly, no testing I am sure the regular expression is wrong, but for help with that look at http://www.zend.com/zend/spotlight/code-gallery-wade5.php and http://www.regular-expressions.info/characters.html Have fun! Link to comment https://forums.phpfreaks.com/topic/58542-random/#findComment-290428 Share on other sites More sharing options...
BillyBoB Posted July 5, 2007 Share Posted July 5, 2007 that would be a good way too... Link to comment https://forums.phpfreaks.com/topic/58542-random/#findComment-290431 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.